I am on the street of the fight here. When I try to add API controllers, it seems to destroy all my base MVC routes and area routes.
At the beginning of my application, I call
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration);
Area configuration example:
namespace **.Areas.Console { public class ConsoleRegistration : AreaRegistration { public override string AreaName { get { return "Console"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Console_default", "Console/Index", new { controller = "base", action = "Index"} ); context.MapRoute( "Console_default2", "Console/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); } } }
My WebApiConfig Class
using System.Web.Http; namespace **.Modules.MVC { public static class WebApiConfig { public static void Register(HttpConfiguration configuration) { configuration.Routes.MapHttpRoute( "API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional } ); } } }
And route class
using System.Web.Mvc; using System.Web.Routing; namespace **.Modules { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.MapRoute( "ErrorHandler", "Error/{action}/{id}", new { controller = "Error", action = "Generic" , id = UrlParameter.Optional } ); // Ignore old Friendly URLS. // These should be removed. routes.IgnoreRoute("Account/Login"); routes.IgnoreRoute(""); routes.IgnoreRoute("Website/{*pathInfo}"); routes.IgnoreRoute("Pages/{*pathInfo}"); routes.IgnoreRoute("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" }); routes.IgnoreRoute("{*allasp}", new { allasp = @".*\.asp(/.*)?" }); routes.IgnoreRoute("{*allhtml}", new { allhtml = @".*\.html(/.*)?" }); routes.IgnoreRoute("{*allphp}", new { allphp = @".*\.php(/.*)?" }); //Default Mvc ignore. routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); //routes.MapRoute( // "404-PageNotFound", // "{*url}", // new { controller = "Error", action = "HttpError" } //); } } }
Now everything works as expected on my local iis, but when I try to access the URLs on my production server, I get the following 404.
When I turn on route debugging, I see this on local.
- Matches the current request: False
- URL: r.ashx / {module} / {* path}
- Default: (null)
- Limitations: (null)
- DataTokens: (null)
- Matches the current query: True
- Url: Control / Index
- Default: controller = base, action = Index
- Limitations: (empty)
- DataTokens: Namespaces = BookEasy.Areas.Control. *, area = Control, UseNamespaceFallback = False
- Matches the current query: True
- Url: Control / {controller} / {action} / {id}
- Default: action = Index, id = UrlParameter.Optional
- Limitations: (empty)
- DataTokens: Namespaces = BookEasy.Areas.Control. *, area = Control, UseNamespaceFallback = False
- Matches the current request: False
- Address: Console / Zip
- Default: controller = base, action = Index
- Limitations: (empty)
- DataTokens: Namespaces = BookEasy.Areas.Console. *, area = Console, UseNamespaceFallback = False
- Matches the current request: False
- Url: Console / {controller} / {action} / {id}
- Default: action = Index, id = UrlParameter.Optional
- Limitations: (empty)
- DataTokens: Namespaces = BookEasy.Areas.Console. *, area = Console, UseNamespaceFallback = False
- Matches the current request: False
- URL: api / {controller} / {id}
- Default: id =
- Limitations: (empty)
- DataTokens: (null)
- Matches the current request: False
- Url: Error / {action} / {id}
- Default: controller = Error, action = Generic, id = UrlParameter.Optional
- Limitations: (empty)
- DataTokens: (empty)
- Matches the current request: False
- Address: Account / Login
- Default: (null)
- Limitations: (empty)
- DataTokens: (null)
- Matches the current request: False
- Address:
- Default: (null)
- Limitations: (empty)
- DataTokens: (null)
- Matches the current request: False
- Address: Website / {* pathInfo}
- Default: (null)
- Limitations: (empty)
- DataTokens: (null)
- Matches the current request: False
- Url: Pages / {* pathInfo}
- Default: (null)
- Limitations: (empty)
- DataTokens: (null)
- Matches the current request: False
- URL: {* allaspx}
- Default: (null)
- Limitations: allaspx = .. aspx (/.)?
- DataTokens: (null)
- Matches the current request: False
- URL: {* allasp}
- Default: (null)
- Limitations: allasp = .. asp (/.)?
- DataTokens: (null)
- Matches the current request: False
- URL: {* allhtml}
- Default: (null)
- Limitations: allhtml = .. html (/.)?
- DataTokens: (null)
- Matches the current request: False
- URL: {* allphp}
- Default: (null)
- Limitations: allphp = .. php (/.)?
- DataTokens: (null)
- Matches the current request: False
- URL: {resource} .axd / {* pathInfo}
- Default: (null)
- Limitations: (empty)
- DataTokens: (null)
- Matches the current request: False
- URL: {* favicon}
- Default: (null)
- Limitations: favicon = (./)?favicon.ico(/.)?
- DataTokens: (null)
- Matches the current query: True
- URL: {* catchall}
- Default: (null)
- Limitations: (null)
- DataTokens: (null)
Sorry for this text wall, it seemed like the best way to show data without a table. https://meta.stackexchange.com/questions/73566/is-there-any-markdown-to-create-tables
Anyway with a question.
And now, what I see when I am debugging routes on my deployed server.
- Matches the current request: False
- URL: r.ashx / {module} / {* path}
- Default: (null)
- Limitations: (null)
- DataTokens: (null)
- Matches the current query: True
- URL: {* catchall}
- Default: (null)
- Limitations: (null)
- DataTokens: (null)
Some other facts.
Mvc version = 4
IIS = 7
When deleting WebApiConfig.Register (GlobalConfiguration.Configuration); the tables are the same (but both skip the api / * route).
So to the point. Why are these two route tables different when adding WebApiConfig.Register?
---------------- EDIT 1 ------------------
As a further note, adding WebApiConfig.Register also breaks my connecting routes: (.