简体   繁体   English

将ASP.NET网站升级到ASP.NET Web应用程序后,Web API 2路由无法正常工作

[英]Web API 2 routing not working after upgrading asp.net website to asp.net web app

I have an asp.net website type project that uses a few Web Api 2 controller. 我有一个使用几个Web Api 2控制器的asp.net网站类型项目。 I converted it to an asp.net web application that supports all three technologies: webforms, web api and mvc. 我将其转换为支持所有三种技术的asp.net Web应用程序:webforms,web api和mvc。

The web api controllers have been moved by the converter (via Project -> Convert to Web Application) to a folder called Old_App_Code. 转换器已将Web API控制器(通过“项目”->“转换为Web应用程序”)移动到名为Old_App_Code的文件夹中。 They were in a App_Code/WebApi folder and now they are in Old_App_Code/WebApi. 它们位于App_Code / WebApi文件夹中,现在位于Old_App_Code / WebApi中。 All the controllers with the exception of one were in a namespace, let's call it name1.name2.name3: 除了一个控制器以外的所有控制器都在名称空间中,我们将其命名为name1.name2.name3:

namespace name1.name2.name3
{
  [RoutePrefix("api/v1/orders")]
  public class OrdersController : BaseAppController //BaseAppController is a class that inherits ApiController and adds a few common methods to all WebApi controllers ...
  {
    ...
  }
}

WebApiConfig.cs (under Old_App_Code): WebApiConfig.cs(在Old_App_Code下):

namespace name1.name2.name3
{
    /// <summary>
    /// </summary>
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {            
          config.MapHttpAttributeRoutes();//use attributes to map routes.

        }
    }
}

global.asax.cs: global.asax.cs:

protected void Application_Start()
{


  GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

  // Code that runs on application startup
  log4net.Config.XmlConfigurator.Configure();

  AreaRegistration.RegisterAllAreas();
  //      GlobalConfiguration.Configure(WebApiConfig.Register);
  GlobalConfiguration.Configure(name1.name2.name3.WebApiConfig.Register);
  FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  RouteConfig.RegisterRoutes(RouteTable.Routes);
  BundleConfig.RegisterBundles(BundleTable.Bundles);

}

RouteConfig.cs: RouteConfig.cs:

public class RouteConfig
{
  public static void RegisterRoutes(RouteCollection routes)
  {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
  }
}

The problem that I have is that the web api routing doesn't work anymore, I get 404s. 我的问题是Web api路由不再起作用,我收到404。

I used the ApiHelp page and the only controller registered is the one that didn't have a namespace to begin with. 我使用了ApiHelp页面,唯一注册的控制器是没有名称空间的控制器。 I put it in a namespace hoping that it won't show up (to get some consistency :-) ), but it still shows up (I restarted IIS Express). 我将其放在一个命名空间中,希望它不会显示(以获得一些一致性:-)),但它仍然显示(我重新启动IIS Express)。

Somehow, MapHttpAttributeRoutes doesn't seem to find these controllers to build the web api routes from. 不知何故,MapHttpAttributeRoutes似乎找不到这些控制器来构建Web api路由。

Any ideas? 有任何想法吗?

I am using VS 2015 with version 5.2.3.0 of System.Web.Mvc and Microsoft.AspNet.WebApi 5.2.3. 我正在将VS 2015与System.Web.Mvc和Microsoft.AspNet.WebApi 5.2.3的5.2.3.0版一起使用。

Update : The controller that works inherits directly from ApiController while the other controllers inherit from a common class (which inherits ApiController) that is in a different project and it is used in another app. 更新 :有效的控制器直接从ApiController继承,而其他控制器则从另一个项目中的通用类(继承ApiController)继承,该通用类在另一个应用程序中使用。 Is there a way to tell the component that registers the controllers that these are WebApi controllers as well? 有没有办法告诉注册控制器的组件,它们也是WebApi控制器? I update the code snippet above to reflect this. 我更新了上面的代码片段以反映这一点。

I figured it out. 我想到了。 The project where the BaseAppController was created referenced version 5.2.2 of the Microsoft.AspNet.WebApi.Core (and the additional libraries) while the main web application referenced version 5.2.3. 创建BaseAppController的项目引用了Microsoft.AspNet.WebApi.Core(和其他库)的5.2.2版本,而主Web应用程序则引用了5.2.3版本。 As soon as I changed the reference to 5.2.3 in that project, the controllers were registered properly. 我在该项目中将引用更改为5.2.3后,就正确注册了控制器。

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

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