将 Web API 添加到现有 MVC Web 应用程序后出现 404 错误 [英] 404 error after adding Web API to an existing MVC Web Application

查看:45
本文介绍了将 Web API 添加到现有 MVC Web 应用程序后出现 404 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个很好的问题: 如何将 Web API 添加到现有的 ASP.NET MVC 4 Web 应用程序项目?

不幸的是,这还不足以解决我的问题.我已经试过两次以确保我没有做错任何事.我右键单击控制器"并添加了项目Web API 2 Controller with actions, using Entity Framework",在那里我选择了我的模型类和数据库上下文.一切顺利...但仍然...每次我尝试访问/api/Rest 时,我都会收到 404 错误(我的控制器的名称是 RestController).

Unfortunately, it wasn't enough to solve my problem. I've tried twice to be sure I haven't done anything wrong. I right clicked on "Controllers" and added the item "Web API 2 Controller with actions, using Entity Framework" where I selected my model class and db context. Everything went fine... but still... everytime I've tried to access /api/Rest I was getting a 404 error (The name of my Controller is RestController).

推荐答案

成功了!!!我不想相信,但你猜怎么着,问题与 Global.asax 路由订单有关.

It's working!!! I didn't want to believe, but guess what, the problem was related with the Global.asax routing order.

虽然它不适用于:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    GlobalConfiguration.Configure(WebApiConfig.Register); //I AM THE 4th
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}      

它适用于:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register); //I AM THE 2nd
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}      

疯了,我知道.

这篇关于将 Web API 添加到现有 MVC Web 应用程序后出现 404 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆