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的更多相关文章

  1. WebAPI HelpPage帮助页

    WebAPI HelpPage是个插件,根据代码的注释生成API说明页,一目了然. 下面开始安装和配置 1.添加引用 先选择管理NuGet程序包,搜索 Microsoft.AspNet.WebApi. ...

  2. MVC和WebApi中设置Area中的页为首页

    拿WebApi为例,我们一般会生成一份帮助文档,帮助文档会在Area中 我们现在要讲帮助文档设为首页 只需在App_Start文件夹下添加 RouteConfig 类 public class Rou ...

  3. ASP.NET MVC]WebAPI应用支持HTTPS的经验总结

    WebAPI应用支持HTTPS的经验总结 在我前面介绍的WebAPI文章里面,介绍了WebAPI的架构设计方面的内容,其中提出了现在流行的WebAPI优先的路线,这种也是我们开发多应用(APP.微信. ...

  4. WebApi 能支持Session

    由于项目实际需要,我希望让WebApi服务也能支持Session,所以便查找资料按照网上的方法开始着手实验. 然后就有了以下的代码,主要是说让WebApi支持Session,要重写Global.asa ...

  5. webapi同时支持post和get报404错误

    文章:webapi设置一个Action同时支持get和post请求 这篇文章,有提供方法.参数前加上[FromUri] [AcceptVerbs("GET", "POST ...

  6. IIS7.5 webapi 不支持 Delete、Put 解决方法

    在IIS管理界面选择API的项目,选择 “Features View”. 2.  选择 “Handler Mappings” 菜单. 3. 打开“WebDAV” 选项. 4. 点击 “Request ...

  7. WebApi Session支持

    代码: WebApiConfig using System; using System.Collections.Generic; using System.Linq; using System.Net ...

  8. webform添加到webapi的支持

    1.添加引用 添加对 System.Net.Http , System.Net.Http.Formatting , System.Web.Http , System.Web.Http.Common , ...

  9. C# asp.net webapi下支持文件下载输出接口

    /// <summary>     /// 下载文件     /// </summary>     public class DownloadController : ApiC ...

随机推荐

  1. .NET-ORM框架EF-Code First代码优先

    前言 Code First顾名思义,通告代码创建实体与数据库.示例中我们会创建表,分表是Studen,Teacher. Code First实战示例 打开VS2013,创建一个项目我这里是用的MVC框 ...

  2. WebBrowser引用IE版本问题,更改使用高版本IE

    做了一个Winform的项目.项目里使用了WebBrowser控件.以前一直都以为WebBrowser是直接调用的系统自带的IE,IE是呈现出什么样的页面WebBrowser就呈现出什么样的页面.其实 ...

  3. Intellij idea 项目目录设置 与包的显示创建

    1.把目录设置成为层级结构显示.和eclipse类似 去掉flatten Packages前面的勾 在项目中创建多级包的时候要注意,必须在Java下建,并且要全输入才能识别

  4. Laravel5性能优化技巧

    分享一些 Laravel 开发的最佳实践,还有调优技巧,后面陆续整理中 1.配置缓存信息 使用laravel自带的artisan命令,将所有config里面的配置都缓存到一个文件里. php arti ...

  5. Centos6.5安装Redis3.0备忘记录

    Centos6.5安装Redis3.0 1. 安装C编译环境 首先需要安装编译Redis的C环境,在命令行执行以下命令: [root@itzhouq32 tools] yum install gcc- ...

  6. Docker 系列七(Dubbo 微服务部署实践).

    一.前言 之前我们公司部署服务,就是大家都懂的那一套(安装JDK.Tomcat —> 编译好文件或者打war包上传 —> 启动Tomcat),这种部署方式一直持续了很久,带来的问题也很多: ...

  7. linux下ftp服务器搭建

    1.yum install vsftpd  使用yum安装ftp 2.创建并授权ftp文件目录   mkdir -P /ftp/ftpadmin       chmod -R 777 /ftp/ftp ...

  8. Vue2+VueRouter2+webpack 构建项目实战(二):目录以及文件结构

    通过上一篇博文<Vue2+VueRouter2+webpack 构建项目实战(一):准备工作>,我们已经新建好了一个基于vue+webpack的项目.本篇文章详细介绍下项目的结构. 项目目 ...

  9. BZOJ2746: [HEOI2012]旅行问题(AC自动机 LCA)

    Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 1188  Solved: 383[Submit][Status][Discuss] Descripti ...

  10. 数据筛选和API优化

    筛选数据 需求:如果数据库中存在OrderNum相同,且IsDefault不同的记录,那么IsDefault值为0的记录将替换值为1的记录(IsDefault值为1的记录不展示). 由于查出来的数据不 ...