简体   繁体   中英

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. I converted it to an asp.net web application that supports all three technologies: webforms, web api and mvc.

The web api controllers have been moved by the converter (via Project -> Convert to Web Application) to a folder called Old_App_Code. They were in a App_Code/WebApi folder and now they are in Old_App_Code/WebApi. All the controllers with the exception of one were in a namespace, let's call it 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):

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:

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:

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.

I used the ApiHelp page and the only controller registered is the one that didn't have a namespace to begin with. 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).

Somehow, MapHttpAttributeRoutes doesn't seem to find these controllers to build the web api routes from.

Any ideas?

I am using VS 2015 with version 5.2.3.0 of System.Web.Mvc and Microsoft.AspNet.WebApi 5.2.3.

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. Is there a way to tell the component that registers the controllers that these are WebApi controllers as well? 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. As soon as I changed the reference to 5.2.3 in that project, the controllers were registered properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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