WebAPI HelpPage支持Area
WebAPI原生的HelpPage文档并不支持Area的生成,需进行如下改造:
WebApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API 配置和服务 // Web API 路由
config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{area}/{controller}/{action}",
defaults: new { id = RouteParameter.Optional }
); //移除XML输出格式
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
}
Areas.HelpPage.ApiDescriptionExtensions:
public static class ApiDescriptionExtensions
{
/// <summary>
/// Generates an URI-friendly ID for the <see cref="ApiDescription"/>. E.g. "Get-Values-id_name" instead of "GetValues/{id}?name={name}"
/// </summary>
/// <param name="description">The <see cref="ApiDescription"/>.</param>
/// <returns>The ID as a string.</returns>
public static string GetFriendlyId(this ApiDescription description)
{
GetAreaName(description); //获取区域名称 string path = description.RelativePath;
string[] urlParts = path.Split('?');
string localPath = urlParts[];
string queryKeyString = null;
if (urlParts.Length > )
{
string query = urlParts[];
string[] queryKeys = HttpUtility.ParseQueryString(query).AllKeys;
queryKeyString = String.Join("_", queryKeys);
} StringBuilder friendlyPath = new StringBuilder();
friendlyPath.AppendFormat("{0}-{1}",
description.HttpMethod.Method,
localPath.Replace("/", "-").Replace("{", String.Empty).Replace("}", String.Empty));
if (queryKeyString != null)
{
friendlyPath.AppendFormat("_{0}", queryKeyString.Replace('.', '-'));
}
return friendlyPath.ToString();
} /// <summary>
/// 获取区域名称
/// </summary>
/// <param name="description"></param>
private static void GetAreaName(this ApiDescription description)
{
//获取controller的fullname
string controllerFullName = description.ActionDescriptor.ControllerDescriptor.ControllerType.FullName;
//匹配areaName
string areaName = Regex.Match(controllerFullName, @"Area.([^,]+)\.C").Groups[].ToString().Replace(".", "");
if (string.IsNullOrEmpty(areaName))
{
//若不是areas下的controller,将路由格式中的{area}去掉
description.RelativePath = description.RelativePath.Replace("{area}/", "");
}
else
{
//若是areas下的controller,将路由格式中的{area}替换为真实areaname
description.RelativePath = description.RelativePath.Replace("{area}", areaName);
}
}
}
Areas.HelpPage.Controllers.HelpController:
public class HelpController : Controller
{
private const string ErrorViewName = "Error"; public HelpController()
: this(GlobalConfiguration.Configuration)
{
} public ActionResult Api(string apiId)
{
if (!String.IsNullOrEmpty(apiId))
{
HelpPageApiModel apiModel = Configuration.GetHelpPageApiModel(apiId);
if (apiModel != null)
{
//防止生成帮助文档时将area作为了Uri参数
foreach (var item in apiModel.UriParameters)
{
if (item.Name.ToLower().Equals("area"))
{
apiModel.UriParameters.Remove(item);
break;
}
}
return View(apiModel);
}
} return View(ErrorViewName);
}
}
WebAPI HelpPage支持Area的更多相关文章
- WebAPI HelpPage帮助页
WebAPI HelpPage是个插件,根据代码的注释生成API说明页,一目了然. 下面开始安装和配置 1.添加引用 先选择管理NuGet程序包,搜索 Microsoft.AspNet.WebApi. ...
- MVC和WebApi中设置Area中的页为首页
拿WebApi为例,我们一般会生成一份帮助文档,帮助文档会在Area中 我们现在要讲帮助文档设为首页 只需在App_Start文件夹下添加 RouteConfig 类 public class Rou ...
- ASP.NET MVC]WebAPI应用支持HTTPS的经验总结
WebAPI应用支持HTTPS的经验总结 在我前面介绍的WebAPI文章里面,介绍了WebAPI的架构设计方面的内容,其中提出了现在流行的WebAPI优先的路线,这种也是我们开发多应用(APP.微信. ...
- WebApi 能支持Session
由于项目实际需要,我希望让WebApi服务也能支持Session,所以便查找资料按照网上的方法开始着手实验. 然后就有了以下的代码,主要是说让WebApi支持Session,要重写Global.asa ...
- webapi同时支持post和get报404错误
文章:webapi设置一个Action同时支持get和post请求 这篇文章,有提供方法.参数前加上[FromUri] [AcceptVerbs("GET", "POST ...
- IIS7.5 webapi 不支持 Delete、Put 解决方法
在IIS管理界面选择API的项目,选择 “Features View”. 2. 选择 “Handler Mappings” 菜单. 3. 打开“WebDAV” 选项. 4. 点击 “Request ...
- WebApi Session支持
代码: WebApiConfig using System; using System.Collections.Generic; using System.Linq; using System.Net ...
- webform添加到webapi的支持
1.添加引用 添加对 System.Net.Http , System.Net.Http.Formatting , System.Web.Http , System.Web.Http.Common , ...
- C# asp.net webapi下支持文件下载输出接口
/// <summary> /// 下载文件 /// </summary> public class DownloadController : ApiC ...
随机推荐
- SQL Server2008附加数据库出现错误
在用SQL Server 2008附加数据库时,出现了如下错误: 解决方法如下: 一. 检查对数据库文件及其所在文件夹是否有操作权限,右键文件属性,将权限修改为完全控制: 二. 权限改了之后,发现还是 ...
- ____利用C#特性Attribute完成对sql语句的拼接
//定义 特性类: public class MyAttribute : Attribute//自定义注解类判断是否是主键 { public bool PrimaryKey = false; publ ...
- WPF TextBlock IsTextTrimmed 判断文本是否超出
WPF TextBlock/TextBox 设置TextTrimming情况下 判断 isTextTrimmed(Text 文本是否超出 是否出现了省略号) private bool IsTextTr ...
- jdk的配置(适用于win7、win8、win10)
一.前言 win7和win8的jdk配置基本一样,所以本文以win7和win10来说明配置. 二.win7jdk环境配置(win8和这个一样) 首先安装好jdk,这里已安装好jdk7,本文采取的是jd ...
- es6 set
ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. const setset = new Set([1, ...
- 简单选择排序算法的C++实现
简单选择排序采用最简单的选择方法,即在剩余序列中选出最小(或最大)的关键字,和剩余序列的第一个关键字交换位置,依次选择下去,直至使整个序列有序. 算法中两层循环的执行次数和初始序列没有关系,第二层循环 ...
- Android为TV端助力 自定义通知栏
package com.example.mvp; import cn.ljuns.temperature.view.TemperatureView;import presenter.ILoginPre ...
- js实现获取当前时间是本月第几周和年的第几周的方法
js实现获取当前时间是本月第几周和年的第几周的方法 获取本月第几周的方法: var getMonthWeek = function (a, b, c) { /** * a = d = 当前日期 * b ...
- 章节四、4-For循环
一.For循环格式 package introduction5; public class ForLoopDemo { public static void main(String[] args) { ...
- Web前端:博客美化:四、网易云音乐单曲播放器
1.页面定制CSS代码 /*3.音乐播放器*/ .content-wrap { overflow-y: scroll; -webkit-overflow-scrolling: touch; } /* ...