1 概要

创建ASP.NET Web Api 时模板自带Help Pages框架。

2 问题

1)使用VS创建Web Api项目时,模板将Help Pages框架自动集成到其中,使得Web Api项目引入了MVC框架开发包,使得项目看起来杂乱。

2)自带的Help Pages框架无法针对Odata控制器生成API文档。

3 问题解决方案

1)独立Help Pages项目,以插件形式添加服务

步骤1,添加类ServiceAssembliesResolver,获得服务集

   /// <summary>
/// 获取插件服务
/// </summary>
public class ServiceAssembliesResolver : DefaultAssembliesResolver
{
public override ICollection<Assembly> GetAssemblies()
{
//获得已有的服务
ICollection<Assembly> baseAssemblies = base.GetAssemblies();
//初始化
List<Assembly> assemblies = new List<Assembly>(baseAssemblies);
//服务插件dll路径
var path = WebConfigSetting.ServicesLocation;
//加载每一个服务插件
foreach (string file in Directory.GetFiles(path, "*.dll"))
{
var controllersAssembly = Assembly.LoadFrom(file);
assemblies.Add(controllersAssembly);
} return assemblies;
}
}

步骤2,替换现有服务

在WebApiConfig.Register方法中添加代码

config.Services.Replace(typeof(IAssembliesResolver), new ServiceAssembliesResolver());

完整代码如下:

namespace HY_WebApi.HelpPages
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API 配置和服务
config.Services.Replace(typeof(IAssembliesResolver), new ServiceAssembliesResolver()); // Web API 路由
config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
); //OData路由,将路由名称设置为控制器(去掉Controller)名称,以便生成Api帮助文档
config.MapODataServiceRoute(
routeName: "ODataSearch",
routePrefix: "odata",
model: GetEdmModel(),
batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer)); }
private static IEdmModel GetEdmModel()
{
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Institution>("ODataSearch"); builder.Namespace = "Search";
//builder.EntityType<Institution>().Collection.Function("GetByIdEq2").Returns<string>();
return builder.GetEdmModel();
} public class Institution
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
}
}

步骤3,添加MultiXmlDocumentationProvider类,读取多个XML文档

   /// <summary>
/// 加载目录下的所有Xml文档
/// </summary>
public class MultiXmlDocumentationProvider : IDocumentationProvider, IModelDocumentationProvider
{
private IList<XmlDocumentationProvider> _documentationProviders; public MultiXmlDocumentationProvider(string xmlDocFilesPath)
{
_documentationProviders = new List<XmlDocumentationProvider>(); foreach (string file in Directory.GetFiles(xmlDocFilesPath, "*.xml"))
{
_documentationProviders.Add(new XmlDocumentationProvider(file));
}
} public string GetDocumentation(HttpParameterDescriptor parameterDescriptor)
{
return _documentationProviders.Select(x => x.GetDocumentation(parameterDescriptor)).FirstOrDefault(x => !string.IsNullOrEmpty(x));
} public string GetDocumentation(Type type)
{
return _documentationProviders.Select(x => x.GetDocumentation(type)).FirstOrDefault(x => !string.IsNullOrEmpty(x));
} //成员导航
public string GetDocumentation(MemberInfo member)
{
return _documentationProviders
.Select(x => x.GetDocumentation(member))
.FirstOrDefault(x => !string.IsNullOrWhiteSpace(x));
} //action 描述
public string GetDocumentation(HttpActionDescriptor actionDescriptor)
{
return _documentationProviders.Select(x => x.GetDocumentation(actionDescriptor)).FirstOrDefault(x => !string.IsNullOrEmpty(x));
} //Controller 描述
public string GetDocumentation(HttpControllerDescriptor controllerDescriptor)
{
return _documentationProviders.Select(x => x.GetDocumentation(controllerDescriptor)).FirstOrDefault(x => !string.IsNullOrEmpty(x));
} public string GetResponseDocumentation(HttpActionDescriptor actionDescriptor)
{
return _documentationProviders.Select(x => x.GetDocumentation(actionDescriptor)).FirstOrDefault(x => !string.IsNullOrEmpty(x)); }
  }

步骤4,使用MultiXmlDocumentationProvider

将config.SetDocumentationProvider(new MultiXmlDocumentationProvider(WebConfigSetting.ServicesLocation));添加到Register方法中

步骤5,创建服务插件文件夹,将服务插件及其XML文档放在文件夹中。

2)重构ApiExplorer,获得Odata控制器的API文档

步骤1,重构ApiExplorer

public class CustomApiExplorer : ApiExplorer
{
private HttpConfiguration configuration;
public CustomApiExplorer(HttpConfiguration configuration)
: base(configuration)
{
this.configuration = configuration;
}
public override bool ShouldExploreController(string controllerVariableValue, HttpControllerDescriptor controllerDescriptor, IHttpRoute route)
{
if (controllerDescriptor == null)
{
throw new ArgumentNullException("controllerDescriptor");
} if (route == null)
{
throw new ArgumentNullException("route");
}
var c = controllerDescriptor.ControllerName;
//获得OData路由
IEdmModel edm = EdmModelCreater.GetEdmModel();
List<string> collectionFromEdms = new List<string>();
foreach (var item in edm.EntityContainer.Elements)
{
collectionFromEdms.Add(item.Name);
} //如果是Odata控制器,那么忽略ApiExplorerSettingsAttribute
ApiExplorerSettingsAttribute setting = controllerDescriptor.GetCustomAttributes<ApiExplorerSettingsAttribute>().FirstOrDefault();
bool isOdataController = collectionFromEdms.Contains(controllerDescriptor.ControllerName);
bool isBaseApi = controllerDescriptor.ControllerName != "BaseApi";
return isBaseApi||isOdataController ||
((setting == null || !setting.IgnoreApi) && MatchRegexConstraint(route, RouteValueKeys.Controller, controllerVariableValue));
} public override bool ShouldExploreAction(string actionVariableValue, HttpActionDescriptor actionDescriptor, IHttpRoute route)
{
if (actionDescriptor == null)
{
throw new ArgumentNullException("actionDescriptor");
} if (route == null)
{
throw new ArgumentNullException("route");
} //获得OData路由
IEdmModel edm = EdmModelCreater.GetEdmModel();
List<string> collectionFromEdms = new List<string>();
foreach (var item in edm.EntityContainer.Elements)
{
collectionFromEdms.Add(item.Name);
} //如果是Odata控制器,那么忽略ApiExplorerSettingsAttribute
ApiExplorerSettingsAttribute setting = actionDescriptor.ControllerDescriptor.GetCustomAttributes<ApiExplorerSettingsAttribute>().FirstOrDefault();
bool isOdataController = collectionFromEdms.Contains(actionDescriptor.ControllerDescriptor.ControllerName);
bool isBaseApi = actionDescriptor.ControllerDescriptor.ControllerName != "BaseApi";
return isBaseApi||isOdataController ||
((setting == null || !setting.IgnoreApi) && MatchRegexConstraint(route, RouteValueKeys.Action, actionVariableValue));
} private static bool MatchRegexConstraint(IHttpRoute route, string parameterName, string parameterValue)
{
IDictionary<string, object> constraints = route.Constraints;
if (constraints != null)
{
object constraint;
if (constraints.TryGetValue(parameterName, out constraint))
{
// treat the constraint as a string which represents a Regex.
// note that we don't support custom constraint (IHttpRouteConstraint) because it might rely on the request and some runtime states
string constraintsRule = constraint as string;
if (constraintsRule != null)
{
string constraintsRegEx = "^(" + constraintsRule + ")$";
return parameterValue != null && Regex.IsMatch(parameterValue, constraintsRegEx, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
}
}
} return true;
}
}

添加RouteValueKeys类

internal static class RouteValueKeys
{
// Used to provide the action and controller name
public const string Action = "action";
public const string Controller = "controller";
}

添加OdataRelativePath类

public static class OdataRelativePath
{
public static void GetOdataRelativePath(CustomApiExplorer customApiExplorer, HttpConfiguration configuration)
{
IEdmModel edm = EdmModelCreater.GetEdmModel();
List<string> collectionFromEdms = new List<string>();
foreach(var item in edm.EntityContainer.Elements)
{
collectionFromEdms.Add(item.Name);
}
Collection<ApiDescription> apiColloction = customApiExplorer.ApiDescriptions;
foreach (ApiDescription api in apiColloction)
{
string controllerName = api.ActionDescriptor.ControllerDescriptor.ControllerName;
//去掉Odata中控制器的版本号
var controllerSelector = configuration.Services.GetService(typeof(IHttpControllerSelector)) as VersionControllerSelector;
string oldString = controllerSelector.RouteVersionSuffixMapping.First(m => m.Key.Contains("OdataRouteVersioning")).Value;
controllerName = controllerName.Replace(oldString, "");
if (collectionFromEdms.Contains(controllerName))
{
string actionName = api.ActionDescriptor.ActionName;
var parameters = api.ActionDescriptor.GetParameters();
string paramStr = null;
foreach (var parameter in parameters)
{
var t = parameter.ParameterType;
if (parameter.ParameterType.IsClass)
{
continue;
}
if (paramStr != null)
{
paramStr = string.Format("{0}&({1}={1})", paramStr, parameter.ParameterName);
}
else
{
paramStr = string.Format("({0}={0})", parameter.ParameterName);
}
}
api.RelativePath = string.Format("{0}/{1}/{2}/Service.{3}{4}", "odata", "{Version}", controllerName, actionName, paramStr);
}
else
{
Regex reg=new Regex("[0-9]");
Match match = reg.Match(api.RelativePath);
if(match.Success)
{
api.RelativePath = api.RelativePath.Replace(string.Format("V{0}",match.Value),"");
}
}
}
}
}

步骤2;根据OData路由拼出api的URI

使用OdataRelativePath.GetOdataRelativePath方法修改ApiExplorer.ApiDescriptions中的URI
例如在控制器中

     public ActionResult Index()
{
ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
CustomApiExplorer customApiExplorer = new CustomApiExplorer(Configuration);
OdataRelativePath.GetOdataRelativePath(customApiExplorer,Configuration);
Collection<ApiDescription> apiDescriptions = new Collection<ApiDescription>();
List<ApiDescription> list = new List<ApiDescription>();
foreach (ApiDescription ad in customApiExplorer.ApiDescriptions)
{
if (ad.ActionDescriptor.ControllerDescriptor.ControllerName != "Metadata" && ad.ActionDescriptor.ActionName != "ToJson")
{
list.Add(ad);
}
}
list = list.OrderBy(m => m.ActionDescriptor.ControllerDescriptor.ControllerName).ToList();
list.ForEach(m =>
{
apiDescriptions.Add(m);
});
return View(apiDescriptions);
}

注意:配置Odata路由时,将路由名称配置为控制器名称(不含Controller字符串),并且编写服务程序时,遵循一个实体对应一个控制器,对应一个Odata路由。

-----------------------------------------------------------------------------------------

转载与引用请注明出处。

时间仓促,水平有限,如有不当之处,欢迎指正。

asp.net web api 构建api帮助文档的更多相关文章

  1. ASP.NET Core WebApi构建API接口服务实战演练

    一.ASP.NET Core WebApi课程介绍 人生苦短,我用.NET Core!提到Api接口,一般会想到以前用到的WebService和WCF服务,这三个技术都是用来创建服务接口,只不过Web ...

  2. Web Api 自动生成帮助文档

    Web Api 自动生成帮助文档   新建Web Api项目之后,会在首页有API的导航菜单,点击即可看到API帮助文档,不过很遗憾,Description 是没有内容的. 怎么办呢? 第一步: 如果 ...

  3. ASP.NET Web Pages (Razor) API Quick Reference

    ASP.NET Web Pages (Razor) API Quick Reference By Tom FitzMacken|February 10, 2014 Print This page co ...

  4. 第三方API接口测试问题反馈文档

    大家在给甲方做大型项目的时候,有时候参与的厂商比较多,而公司负责的部分又需要第三方厂商提供接口支持. 例如我们做医疗行业的,给医院提供医保控费系统服务的,就需要HIS厂商提供接口给我们采集数据.有时候 ...

  5. ASP.NET WebAPI使用Swagger生成测试文档

    ASP.NET WebAPI使用Swagger生成测试文档 SwaggerUI是一个简单的Restful API测试和文档工具.简单.漂亮.易用(官方demo).通过读取JSON配置显示API .项目 ...

  6. Java实现web在线预览office文档与pdf文档实例

    https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX 摘要: 本文讲的是Java实现web在线预览office文档 ...

  7. (转)WEB页面导出为Word文档后分页&横向打印的方法

    <html>    <HEAD>        <title>WEB页面导出为Word文档后分页&横向打印的方法 </title>    < ...

  8. Jenkins构建项目帮助文档

    Jenkins构建项目帮助文档 主要步骤 一.配置jdk 1.1.   下载jdk,安装到自己电脑磁盘的Java目录下(比如:D:\Java\jdk). 1.2.   Jdk环境变量的配置: 1. 鼠 ...

  9. Web 前端 UI 组件库文档自动化方案 All In One

    Web 前端 UI 组件库文档自动化方案 All In One 需求 自动化 动态 好用 markdown element-ui 中示例和说明按照一定规则写在md文件中,调用md-loader将md文 ...

随机推荐

  1. 虚拟机VMware-workstation-full-12.5.2安装注册方法(附安装包下载链接)

    首先,需要下载好虚拟机的安装包: 链接:http://pan.baidu.com/s/1mix3fFa 密码:6ftu 下载好后,双击安装程序,弹出安装界面,然后按照红色圈圈的指示进行安装操作,完成安 ...

  2. LeetCode 538. Convert BST to Greater Tree (把二叉搜索树转换成较大树)

    Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...

  3. JAVA提高九:集合体系

    在经过了前面的JDK6.0新特性的学习后,将进一步深入学习JDK,因为集合的重要性,因此从集合开始入手分析: 一.集合概况 Java是一种面向对象语言,如果我们要针对多个对象进行操作,那么首先必要将多 ...

  4. Django1.11搭建一个简易上传显示图片的后台

    本文为作者原创,转载请注明出处(http://www.cnblogs.com/mar-q/)by 负赑屃 项目展示需要,之前没研究过Django,网上查资料快速做了一个后台,写下了防止自己忘了. p ...

  5. RobotFrameWork安装笔记

    1.   RobotFrameWork安装配置笔记 1.1. 安装环境 64位win10家庭中文版 网上很多这方面的教程,但是比较零散,这里是自己安装配置的一个简单的笔记. 1.2. 安装说明 由于R ...

  6. Django web框架篇:基础

    对于web开发者来说,socket是基础.因为Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. 对于真实开发中的python web程序来说,一般会分为两 ...

  7. Chinese Rings

    Chinese Rings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  8. .Net Core在Ubuntu上操作MySql折腾实录

    .Net Core 2.0 发布也这么久了,一直想着折腾着玩玩,无奈一直没时间,这几天准备开始好好学习下C#在跨平台方面的应用,记录下来以备自己以后回忆.学习. 本篇博客的主要内容: MySql在Ub ...

  9. js 添加事件 attachEvent 和 addEventListener 的区别

    1.addEventListener 适用w3c标准方法addEventListener绑定事件,如下,事件的执行顺序和绑定顺序一致,执行顺序为method1->method2->meth ...

  10. Spring IOC容器分析(2) -- BeanDefinition

    上文对Spring IOC容器的核心BeanFactory接口分析发现:在默认Bean工厂DefaultListableBeanFactory中对象不是以Object形成存储,而是以BeanDefin ...