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 ...
随机推荐
- Zepto整体概况
var Zepto = (function(){ var zepto = {},$; zepto.Z = function(dom, selector) { return new Z(dom,sele ...
- 第一册:lesson seventy three.
原文: The way to King Street. Last week Mrs. Mills went to London. She does not know London very well, ...
- 【转载】C#常用数据库Sqlserver中DATEPART() 函数
在Sqlserver数据库中,DATEPART() 函数用于返回日期/时间的单独部分,比如年.月.日.小时.分钟等等.DatePart()函数的语法为: DATEPART(datepart,date) ...
- 【原创】MVC+ZTree实现权限树的功能
今天自己采用MVC+ZTree的技术实现权限树的功能,有需要的可以收藏一下. 1.需要引用的JS 文件 <link href="~/Content/ZTree/css/demo.css ...
- SQL--server事物
事物 特点: 1.原子性:事物必须是一个自动工作的单元, 2.一致性:事物结束的时候,所有内部数据都是正确的 3.隔离性:并发多个事物时,各个事物不干涉内部数据,处理的都是另外一个事物处理之前或之后的 ...
- curl模拟ip和来源进行网站采集的实现方法
对于限制了ip和来源的网站,使用正常的采集方式是不行的.这里说我的一种方法吧,使用php的curl类实现模拟ip和来源,可以实现采集限制ip和来源的网站. 1.设置页面限制ip和来源访问比如服务端的s ...
- 蓝桥杯试题----- 打印大X
打印大X 小明希望用星号拼凑,打印出一个大X,他要求能够控制笔画的宽度和整个字的高度.为了便于比对空格,所有的空白位置都以句点符来代替. 要求输入两个整数m n,表示笔的宽度,X的高度.用空格分开(0 ...
- Python-10行代码实现3个数据可视化
阅读本文约“1分钟” 最近将Python作为第二编程语言,进行了了解与学习,可以说它的包是很强大的.这次的demo仅仅不到10行代码就可以实现三个数据可视化的小实例. 我们将要使用到matplotli ...
- js循环json得到 键和值
var jsondata=[{"男":4,"女":3,"不详":0},{"男one":23,"女two&quo ...
- 课程作业——熟悉常用的Linux操作
cd命令:切换目录 (1) 切换到目录 /usr/local cd /usr/local (2) 去到目前的上层目录 cd .. (3) 回到自己的主文件夹 cd ~ ls命令:查看文件与目录 (4) ...