使用AttributeRouting在URL中隐藏区域 [英] Hide Area in URL using AttributeRouting

查看:94
本文介绍了使用AttributeRouting在URL中隐藏区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Areas版本化用AttributeRouting用ASP.NET MVC3编写的API来定义我们的路由。

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

现在,我们有一个 v1区域,这是我们的第一个API版本。到达v2时,我们将复制v1并进行修改。

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.

我想为网站使用相同的版本控制,并且我不想在路由中使用/ v1。

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

我的问题是,如何在URL中隐藏该区域,以便我可以拨打电话

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

mywebsite.com/Users/1 

而不是

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();
        }

    }

这就是我得到的我尝试访问mywebsite.com/

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

预先感谢!

推荐答案

执行以下操作:

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

默认情况下,区域以区域名称为前缀。 AreaUrl属性使您可以覆盖它。我将在此处更新Wiki: https://github.com/mccalltd/AttributeRouting/wiki/Areas

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

很抱歉造成混淆!

此外,您不应在以下位置添加正斜杠通过AR定义的所有网址的开头或结尾。您的堆栈跟踪转储会突出显示MVC在名为 /的文件夹中寻找视图。如果您想要一个空的URL,只需使用。

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

这篇关于使用AttributeRouting在URL中隐藏区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆