webapi 路由限制命名控件_解决Web API路由配置支持Area及命名空间参数
usingSystem;usingSystem.Collections.Concurrent;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Net;usingSystem.Net.Http;usingSystem.Web;usingSystem.Web.Http;usingSystem.Web.Http.Controllers;usingSystem.Web.Http.Dispatcher;namespace_1_解决MVC的Controller和Web_API的Controller类名不能相同的问题.App_Start.webExt
{public classNamespaceHttpControllerSelector : DefaultHttpControllerSelector
{private const string NamespaceRouteVariableName = "namespaceName";private readonlyHttpConfiguration _configuration;private readonly Lazy>_apiControllerCache;publicNamespaceHttpControllerSelector(HttpConfiguration configuration)
:base(configuration)
{
_configuration=configuration;
_apiControllerCache= new Lazy>(new Func>(InitializeApiControllerCache));
}private ConcurrentDictionaryInitializeApiControllerCache()
{
IAssembliesResolver assembliesResolver= this._configuration.Services.GetAssembliesResolver();var types = this._configuration.Services.GetHttpControllerTypeResolver().GetControllerTypes(assembliesResolver).ToDictionary(t => t.FullName, t =>t);return new ConcurrentDictionary(types);
}public IEnumerable GetControllerFullName(HttpRequestMessage request, stringcontrollerName)
{objectnamespaceName;var data =request.GetRouteData();
IEnumerable keys = _apiControllerCache.Value.ToDictionary, string, Type>(t =>t.Key,
t=>t.Value, StringComparer.CurrentCultureIgnoreCase).Keys.ToList();if (!data.Values.TryGetValue(NamespaceRouteVariableName, outnamespaceName))
{return from k inkeyswhere k.EndsWith(string.Format(".{0}{1}", controllerName, DefaultHttpControllerSelector.ControllerSuffix), StringComparison.CurrentCultureIgnoreCase)selectk;
}//get the defined namespace
string[] namespaces = (string[])namespaceName;return from n innamespaces
join kin keys on string.Format("{0}.{1}{2}", n, controllerName, DefaultHttpControllerSelector.ControllerSuffix).ToLower() equals k.ToLower()selectk;
}public overrideHttpControllerDescriptor SelectController(HttpRequestMessage request)
{
Type type;if (request == null)
{throw new ArgumentNullException("request");
}string controllerName = this.GetControllerName(request);if (string.IsNullOrEmpty(controllerName))
{throw newHttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound,string.Format("No route providing a controller name was found to match request URI '{0}'", new object[] { request.RequestUri })));
}
IEnumerable fullNames =GetControllerFullName(request, controllerName);if (fullNames.Count() == 0)
{throw newHttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound,string.Format("No route providing a controller name was found to match request URI '{0}'", new object[] { request.RequestUri })));
}if (this._apiControllerCache.Value.TryGetValue(fullNames.First(), outtype))
{return newHttpControllerDescriptor(_configuration, controllerName, type);
}throw newHttpResponseException(request.CreateErrorResponse(HttpStatusCode.NotFound,string.Format("No route providing a controller name was found to match request URI '{0}'", new object[] { request.RequestUri })));
}
}
}
webapi 路由限制命名控件_解决Web API路由配置支持Area及命名空间参数相关推荐
- webapi 路由限制命名控件_什么是命名数据网络NDN?
命名数据网络(Named Data Networking, NDN)经常出现在5G.边缘计算相关的文献书籍上,那NDN究竟是何方神圣?一起来了解一下吧! 一.NDN架构概述 众所周知,21世纪的重要特 ...
- ASP.NET Web API 路由对象介绍
前言 在ASP.NET.ASP.NET MVC和ASP.NET Web API这些框架中都会发现有路由的身影,它们的原理都差不多,只不过在不同的环境下作了一些微小的修改,这也是根据每个框架的特性来制定 ...
- Android 软键盘弹出时把布局顶上去,控件乱套解决方法
Android 软键盘弹出时把布局顶上去,控件乱套解决方法 参考文章: (1)Android 软键盘弹出时把布局顶上去,控件乱套解决方法 (2)https://www.cnblogs.com/zhuj ...
- Android Studio Design界面不显示layout控件的解决方法
Android Studio Design界面不显示layout控件的解决方法 参考文章: (1)Android Studio Design界面不显示layout控件的解决方法 (2)https:// ...
- VS2008 中无法使用ACTIVEX控件的解决
转]VS2008 中无法使用ACTIVEX控件的解决 如果你在打开对话框时弹 出 "The ActiveX control "Grid Control" could no ...
- matlab创建控件失败win7,Win7系统无法安装ActiveX控件的解决方法
Win7系统无法安装ActiveX控件怎么办?ActiveX控件是网站常用的一款网页辅助工具,有时候我们可能需要安装它,但是却发现浏览器阻止了它安装,那么当你遇到这种情况该怎么办呢?下面小编就给大家带 ...
- 锐洋java web打印控件_锐洋Java web打印控
ReYoPrint产品介绍 ReYoPrint (锐洋.打印控件)是一款实现网页套打的专用工具.作为web应用开发者,我们经常会遇到在浏览器中打印报表.票据的需求,这些需求浏览器本身的打印功能一般不能 ...
- CADEditorX新控件_可进君羊交流与学习
CADEditorX是一个ActiveX组件,用于在支持ActiveX和COM技术的任何开发环境中(例如C#,Visual C ++,Delphi,VB,JavaScript等)将CAD功能添加到网页 ...
- java web 打印控件_web打印,web打印控件,dotnet web打印控件,java web打印控件,webprint...
webprint打印参数设置小插件,主要实现打印纸张,边距,打印机等的设置. 主要功能: 设置页眉页脚属性 设置打印页面左右上下边距 设置打印方向,打印份数,打印指定页 打印模版和指定纸型绑定 设置自 ...
最新文章
- svn提示服务器禁止修改目录,SVN Eclipse插件中如何忽略对服务器已有文件修改后的提交...
- 通过递归算法完成树的级联勾选的一般思路
- 服务提供商应该如何帮助企业保护数据安全
- 一致性 Hash 算法原理总结
- ReactNative(API)Alert
- 计算机考研备考指南,计算机专业考研复习指南篇
- WebClient与WebRequest差异
- MySQL和PostgreSQL在多表连接算法上的差异
- 快速掌握用python处理Excel
- spring事务和mysql的隔离级别
- servlet过滤器入门
- linux下nand flash驱动工作原理,1.3.4. Nand flash驱动工作原理
- 用oledb导出数据到excel
- 手机 com.android.backupconfirm可以删除吗,手机上什么文件可以删除?答案都在这里了...
- 【解题报告】【HODJ1231】【最大子序列和】最大连续子序列
- 将war文件解压到指定目录
- 灰灰考研机试班 | 基础练习二 问题 B: 日历本 | 入门题-模拟 | 日期、星期相关计算
- 怎样选择合适的ADC芯片
- 推荐三款动态壁纸软件,足够让你的桌面惊艳!
- C语言--实现状态机
热门文章
- java有道翻译_Java 调用 有道翻译API
- java 输出中文_没见过的 Java 编程入门教程!例程使用中文标识符代码:问个好吧...
- 苹果safari关掉java_如何在Safari,Chrome,Firefox和系统范围内禁用Java | MOS86
- c++hello world代码_在Rust代码中编写Python是种怎样的体验?
- seo从入门到精通_新手学习SEO一个月能学会吗?
- 浓浓中国风的雅致新年元旦PSD分层海报模板
- 疫情海报模板|光效显微传播大数据必备psd素材
- UI初学者必备知识|最好的Sketch App 素材资源都在一流设计导航
- UI帮手|小样机大作用
- libcare Hello World测试例