c# – ASP.net web api 2 Route-Attribute无法正常工作
发布时间:2020-12-15 19:47:22 所属栏目:百科 来源:网络整理
导读:我有以下问题,我的路由属性不起作用. 我有以下行动: [HttpGet][Route("~api/admin/template/{fileName}")]public HttpResponseMessage Template(string fileName){ return CreateHtmlResponse(fileName);} 我想访问像… / api / admin / template / login.h
我有以下问题,我的路由属性不起作用.
我有以下行动: [HttpGet] [Route("~api/admin/template/{fileName}")] public HttpResponseMessage Template(string fileName) { return CreateHtmlResponse(fileName); } 我想访问像… / api / admin / template / login.html这样的动作,以便模板get login.html作为文件名传递. 但我总是得到:没有找到与请求URI’http:// localhost:50121 / api / admin / template / login.html’匹配的HTTP资源. 以下请求有效:/api/admin/template?fileName=login.html 有谁知道,我的路由错误了什么? 编辑: 我的路线配置 config.Routes.MapHttpRoute( "API Default","api/{controller}/{action}",new { id = RouteParameter.Optional }); 解决方法
您必须调用MapHttpAttributeRoutes(),以便框架能够遍历您的属性并在应用程序启动时注册适当的路由:
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.MapHttpAttributeRoutes(); // you can add manual routes as well //config.Routes.MapHttpRoute(... } } 见MSDN (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |