我可以以某种方式获取WebApiConfig的路由映射中的控制器名称吗?
我想要做的是将控制器名称的一部分与动作匹配。
config.Routes.MapHttpRoute(
name: "ApiGetAllDefault",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional, action = "Get" + controllerName + "s" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);
更新:所有这一切的原因是我以
的方式进行路由config.Routes.MapHttpRoute(
name: "ApiById",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { id = @"^[0-9]+$" }
);
config.Routes.MapHttpRoute(
name: "ApiByActionId",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: null,
constraints: new { id = @"^[0-9]+$" }
);
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}",
defaults: null
);
/* Default route */
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
包含操作名称的api/World/GetWorld/5
之类的请求会匹配,但api/World/5
之类的其他请求不会或分别与多个操作匹配。由于我没有在路由和动作之间找到正确的默认映射,我有想法为所有控制器定义更具体的路由。
foreach (Type t in GetTypesInNamespace(Assembly.GetExecutingAssembly(), "MyProject.Controllers")) {
if (typeof(ApiController).IsAssignableFrom(t)) { // Make sure that this controller class is deriving from api controller
string controllerName = t.Name.Substring(0, t.Name.LastIndexOf("Controller")); // Remove Controller postfix from name
config.Routes.MapHttpRoute(
name: "ApiGet" + controllerName,
routeTemplate: "api/" + controllerName + "/{id}",
defaults: new { controller = controllerName, action = "Get" + controllerName },
constraints: new { id = @"^[0-9]+$", httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);
config.Routes.MapHttpRoute(
name: "ApiGet" + controllerName + "s",
routeTemplate: "api/" + controllerName + "/{id}",
defaults: new { controller = controllerName, id = RouteParameter.Optional, action = "Get" + controllerName + "s" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);
}
}
此解决方案对我有用,但我仍然想知道是否存在通用路由,以便将没有操作名称的reuqest与默认操作匹配。
答案 0 :(得分:3)
你可以像在WebApiConfig
中定义控制器+动作一样config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
OR
您可以查看
routes.MapHttpRoute("DefaultApiWithId", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d+" });
routes.MapHttpRoute("DefaultApiWithAction", "Api/{controller}/{action}");
routes.MapHttpRoute("DefaultApiGet", "Api/{controller}", new { action = "Get" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
routes.MapHttpRoute("DefaultApiPost", "Api/{controller}", new {action = "Post"}, new {httpMethod = new HttpMethodConstraint(HttpMethod.Post)});
更多信息请访问Routing in ASP.NET Web API
答案 1 :(得分:2)
如果您要限制一个控制器,那么您确实喜欢以下操作。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new {controller = "FrontEnd" }
);
constraints
仅允许FrontEnd控制器