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; } }
,相关文章
- velocity的string转数字,numberTool
- 7. Reverse Words in a String
- Incorrect string value: ‘ç飑 for column ‘XXX‘
- String s1 = new String("abc"); String s2 = ("abc");
- String s1=new String("abc"); 和String s1="abc"区别
- python3中格式化输出时报错unsupported format string passed to bytes.__format__
- (Java)String截取指定字符前面(后面)所有字符和String的常用方法
- 解决C++遇到的未定义标识符 “string“、未定义标识符 “cout“、“name”: 未知重写说明符错误
- printf如何输出string类型,及c_str函数部分要点
- VS2010中的编译错误:error C2061: 语法错误: 标识符"string"