asp.net-mvc – 所有ASP.NET Web API控制器返回404
发布时间:2020-12-15 19:06:49 所属栏目:asp.Net 来源:网络整理
导读:我试图获得一个API控制器在ASP.NET MVC 4网络应用程序内部工作。然而,每个请求导致一个404,我被解雇。 :/ 我有从项目模板定义的标准API控制器路由,如: public static class WebApiConfig{ public static void Register(HttpConfiguration config) { con
我试图获得一个API控制器在ASP.NET MVC 4网络应用程序内部工作。然而,每个请求导致一个404,我被解雇。 :/
我有从项目模板定义的标准API控制器路由,如: public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi",routeTemplate: "api/{controller}/{id}",defaults: new { id = RouteParameter.Optional } ); } } 在Global.asax中调用注册: protected void Application_Start() { AreaRegistration.RegisterAllAreas(); // Register API routes WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); } 我有一个基本的API控制器这样: namespace Website.Controllers { public class FavoritesController : ApiController { // GET api/<controller> public IEnumerable<string> Get() { return new [] { "first","second" }; } // PUT api/<controller>/5 public void Put(int id) { } // DELETE api/<controller>/5 public void Delete(int id) { } } } 现在,当我浏览到localhost:59900 / api / Favorites我希望Get方法被调用,而是我得到一个404状态代码和以下响应: <Error> <Message> No HTTP resource was found that matches the request URI 'http://localhost:59900/api/Favorites'. </Message> <MessageDetail> No type was found that matches the controller named 'Favorites'. </MessageDetail> </Error> 任何帮助将非常感谢,我失去了我的想法有点在这里。 :) 谢谢! 解决方法
我遇到的一个事情是我的配置注册在错误的顺序在我的GLobal.asax文件例如:
正确的顺序: AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); 错误订单: AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); WebApiConfig.Register(GlobalConfiguration.Configuration); 只是说,这是我的问题,改变顺序是显而易见的,但有时被忽视,可以造成很多挫折。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – ASP.NET MVC – 从URL中删除控制器名称
- asp.net – 有没有像ELMAH axd for log4net来监控日志?
- 有AsIFnet标记的#IF DEBUG吗?
- asp.net-mvc-3 – 无法加载文件或程序集’StructureMap,Ver
- asp.net字符串分割函数使用方法分享
- asp.net-mvc – 什么是ASP.Net MVC?
- asp.net-mvc – asp.net MVC应该是View-Model封装域模型吗?
- asp.net-mvc – Asp.Net Mvc – 在ActionResult.Execute之前
- asp.net-core – ASP.Net 5类库中的EntityFramework命令?
- AllowOnlyAlphanumericUserNames – 如何设置? (RC到RTM破
推荐文章
站长推荐
- asp.net-mvc-4 – 多个单选按钮组在MVC 4剃刀
- asp.net-core – appsettings.json中ConnectionS
- asp.net – 在IIS URL Rewrite 2中重新处理重写的
- asp.net-mvc-3 – 如何将模型绑定中使用的所有Va
- asp.net-mvc-3 – 在Html.BeginForm()中使用DELE
- asp.net – 如何隐藏web api 2中的元数据,odata
- ASP.NET导出到Excel – 锁定单元格
- asp.net-mvc – 如何在ASP.NET MVC中执行辅助操作
- asp.net-mvc – 在ASP.NET MVC项目上使用远程Web
- ASP.NET – 消失的会话变量
热点阅读