Search code examples
c#asp.net-mvc-3routesasp.net-mvc-routingattributerouting

Hide Area in URL using AttributeRouting


We are using Areas to version an API written in ASP.NET MVC3 with AttributeRouting to define our routes.

Right now we have a "v1" area that is our first version of API. When we got to v2, we will copy over v1 and make modifications.

I want to use the same versioning for a website and I don't want the /v1 in the route.

My question is, how do I hide the Area in my URL so I can call

mywebsite.com/Users/1 

instead of

mywebsite.com/v1/Users/1

Here is what I have in my controller

    [RouteArea("/")]
    public class HomeController : Controller
    {
        //
        // GET: /v1/Home/
        [GET("")]
        public ActionResult Index()
        {
            return View();
        }

    }

and here is what I get when I try to visit mywebsite.com/

error

Thanks in advance!


Solution

  • Do this:

    [RouteArea("AreaName", AreaUrl = "")]
    

    By default, areas are prefixed with the area name. The AreaUrl property lets you override that. I'll update the wiki here: https://github.com/mccalltd/AttributeRouting/wiki/Areas

    Sorry for the confusion!

    Also, you shouldn't add forward-slashes at the beginning or end of any urls defined via AR. Your stack trace dump highlights that MVC is looking for views in a folder named "/". If you want an empty url, just use "".