简体   繁体   English

将Web API添加到现有MVC Web应用程序后出现404错误

[英]404 error after adding Web API to an existing MVC Web Application

There's a great question here: How to add Web API to an existing ASP.NET MVC 4 Web Application project? 这里有一个很好的问题: 如何将Web API添加到现有的ASP.NET MVC 4 Web应用程序项目中?

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. 我右键单击“控制器”并添加了项目“Web API 2 Controller with actions,using Entity Framework”,其中我选择了我的模型类和db上下文。 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). 一切都很顺利...但仍然...每次我试图访问/ api / Rest我得到404错误(我的控制器的名称是RestController)。

It's working!!! 它的工作!!! I didn't want to believe, but guess what, the problem was related with the Global.asax routing order . 我不想相信,但猜猜是什么,问题与Global.asax路由顺序有关

While it doesn't work with: 虽然它不适用于:

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

It works with: 它适用于:

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

Crazy, I know. 疯了,我知道。

If you want to use WebAPI inside an existing MVC (5) project you have to do the following steps: 如果要在现有MVC(5)项目中使用WebAPI,则必须执行以下步骤:
1.Add WebApi packages: 1.添加WebApi包:

Microsoft.AspNet.WebApi
Microsoft.AspNet.WebApi.Client
Microsoft.AspNet.WebApi.Core
Microsoft.AspNet.WebApi.WebHost
Newtonsoft.Json

2.Add WebApiConfig.cs file to App_Start folder: WebApiConfig.cs文件添加到App_Start文件夹:

using System.Web.Http;

namespace WebApiTest
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

3.Add the following line to Glabal.asax : Glabal.asax下行添加到Glabal.asax

GlobalConfiguration.Configure(WebApiConfig.Register);

Important note: you have to add above line exactly after AreaRegistration.RegisterAllAreas(); 重要说明:您必须在AreaRegistration.RegisterAllAreas();之后添加以上行AreaRegistration.RegisterAllAreas();

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

"When adding new routes ALWAYS KEEP IN MIND that you have to add specific route on the top followed by more generic route in the end. Otherwise, your web app will never receive proper routing." “当添加新路线总是保持心态时,您必须在顶部添加特定路线,然后在最后添加更通用的路线。否则,您的网络应用将永远不会收到正确的路由。”

The above is the citation from here: http://www.codeproject.com/Tips/771809/Understanding-the-Routing-Framework-in-ASP-NET-MVC 以上是此处引用的内容: http//www.codeproject.com/Tips/771809/Understanding-the-Routing-Framework-in-ASP-NET-MVC

I know the answer is already given, but this could help to understand why we need to put GlobalConfiguration.Configure(WebApiConfig.Register); 我知道已经给出了答案,但这有助于理解为什么我们需要放置GlobalConfiguration.Configure(WebApiConfig.Register); before RouteConfig.RegisterRoutes(RouteTable.Routes); 在RouteConfig.RegisterRoutes(RouteTable.Routes)之前;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM