zoukankan      html  css  js  c++  java
  • 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[0];
                string queryKeyString = null;
                if (urlParts.Length > 1)
                {
                    string query = urlParts[1];
                    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[1].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);
            }
        }
  • 相关阅读:
    [转载][QT][SQL]sql学习记录7_sqlite 日期 & 时间
    [转载][QT][SQL]sql学习记录6_sqlite Autoincrement(自动递增)
    [转载][QT][SQL]sql学习记录5_sqlite视图(View)
    [转载][QT][SQL]sql学习记录4_sqlite约束
    [转载][QT][SQL]sql学习记录3_sqlite之update delete like
    [转载][QT][SQL]sql学习记录2_sqlite数据类型
    [转载][QT][SQL]sq]学习记录1_模糊搜索
    [svn]显示日志很慢 点击文件查看更改记录也贼慢
    [QT]给QApplication安装事件过滤器 app.installEventFilter
    [QT]数据库SQLITE使用错误记录
  • 原文地址:https://www.cnblogs.com/xiaoxiaoyu0707/p/9700596.html
Copyright © 2011-2022 走看看