Hide Area in URL using AttributeRouting

走远了吗. 提交于 2020-01-14 13:37:49

问题


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/

Thanks in advance!


回答1:


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 "".



来源:https://stackoverflow.com/questions/10215144/hide-area-in-url-using-attributerouting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!