locked
why Web API 2 has RouteConfig.cs file? RRS feed

  • Question

  • I m working on Web API project, and it is using WebApiConfig.cs file to defined routs for Web API.

    Web API fold RouteConfig.cs file. I have googled and they say RouteConfig.cs is for MVC routing. It is actually a confusing statement. Can you please guide why WebAPI has RouteConfig.cs file ? Why would one need MVC routing in Web API ? What function it will do ?

    Thanks for your help and guidance.

    • Moved by Kristin Xie Wednesday, January 7, 2015 5:47 AM
    Tuesday, January 6, 2015 8:53 AM

Answers

  • The RouteConfig.cs file isn't required for Web API. It is included by default in new Web API projects because that template also includes an MVC webpage (for the index page that appears when you open the project in a browser).

    If you delete both the RouteConfig.cs and the HomeController.cs file (as well as modify Global.asax.cs to not call RouteConfig.RegisterRoutes()), then your project will still compile and the API controllers will still work fine, you will just be missing a pretty homepage.

    Note also that MVC Areas have their own MVC routing and do not use the global MVC routing, which is why the /Help page still works even when RouteConfig.cs has been removed.

    • Marked as answer by Elite.DOA Thursday, January 8, 2015 12:58 PM
    Thursday, January 8, 2015 12:00 AM

All replies

  • For questions related to ASP.NET use the ASP.NET forum   

    MVC forum

    http://forums.asp.net/1146.aspx


    You should get more, better and faster answers on the other forum.  Thanks, ahead of time.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, January 7, 2015 5:47 AM
  • The RouteConfig.cs file isn't required for Web API. It is included by default in new Web API projects because that template also includes an MVC webpage (for the index page that appears when you open the project in a browser).

    If you delete both the RouteConfig.cs and the HomeController.cs file (as well as modify Global.asax.cs to not call RouteConfig.RegisterRoutes()), then your project will still compile and the API controllers will still work fine, you will just be missing a pretty homepage.

    Note also that MVC Areas have their own MVC routing and do not use the global MVC routing, which is why the /Help page still works even when RouteConfig.cs has been removed.

    • Marked as answer by Elite.DOA Thursday, January 8, 2015 12:58 PM
    Thursday, January 8, 2015 12:00 AM