string parameterName

时间:2022-06-01 08:41:35

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; } }