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 ...
随机推荐
- java 开发 websocket 网页端聊天室
博客地址:https://ainyi.com/67 WebSocket协议是基于TCP的一种新的网络协议.它实现了浏览器与服务器全双工(full-duplex)通信——允许服务器主动发送信息给客户端. ...
- 一个word文档中,多个表格的批量调整(根据窗口调整表格和添加表格水平线)
Sub 自动调整所有表格() ' ' 自动调整所有表格 宏 ' 'Application.Browser.Target = wdBrowseTable For i = 1 To ActiveDocum ...
- SpringBoot之前端文件管理
WebJars能使Maven的依赖管理支持OSS的JavaScript库/CSS库,比如jQuery.Bootstrap等. (1)添加js或者css库 pom.xml <dependency& ...
- 从零开始学安全(二十)●PHP辅助函数
- SQL 用于各种数据库的数据类型(转载) sqlserver 数据类型 取值范围 长度
SQL 用于各种数据库的数据类型 来源 http://www.runoob.com/sql/sql-datatypes.html 面向数据库编程中,数据类型的取值范围.长度,可能是需要经常查看的 ...
- [PHP]代码执行和生命周期
PHP代码的执行:1.和大部分程序一样,接收数据,处理数据,输出结果2.编写的代码就是输入的数据,php内核进行处理,返回相应的输出3.php作为业务程序和编译语言的区别就是,php多了一步把用户代码 ...
- 26.C++- 泛型编程之类模板(详解)
在上章25.C++- 泛型编程之函数模板(详解) 学习了后,本章继续来学习类模板 类模板介绍 和函数模板一样,将泛型思想应用于类. 编译器对类模板处理方式和函数模板相同,都是进行2次编译 类模板通 ...
- C#设计模式之九组合模式(Composite Pattern)【结构型】
一.引言 今天我们要讲[结构型]设计模式的第四个模式,该模式是[组合模式],英文名称是:Composite Pattern.当我们谈到这个模式的时候,有一个物件和这个模式很像,也符合这个模式要表达的意 ...
- Ext.isEmpty()的使用
说明如下: isEmpty( Object value, Boolean allowEmptyString ) : Boolean 如果传递的值为空,则返回 true,否则返回 false.该值被认为 ...
- Statement和PreparedStatement的异同
1.首先两个都是java向数据库执行sql语句的对象! java代码连接数据库,并且执行sql语句的步骤如下: //1.注册数据库的驱动程序 Class.forName(driverClass); / ...