【问题标题】:How to convert this fix this ASP.NET MVC5 route?如何转换此修复此 ASP.NET MVC5 路由?
【发布时间】:2013-11-15 19:14:27
【问题描述】:

我不确定如何将此 AttributeRoute 转换为 MVC5 路由。

[GET("", IsAbsoluteUrl = true)] // => main home page.
[GET("Index")]
public ActionResult Index(..) { .. }

IsAbsoluteUrl 是让我感到困惑的事情之一。

【问题讨论】:

    标签: c# .net asp.net-mvc-routing asp.net-mvc-5 attributerouting


    【解决方案1】:

    根据此处找到的注释:http://attributerouting.net/#route-prefixesIsAbsoluteUrl 标志旨在忽略控制器上定义的RoutePrefix。例如:

    [RoutePrefix("MyApp")]
    public class MyController : Controller {
    
        [GET("", IsAbsoluteUrl = true)] //1
        [GET("Index")] //2
        public ActionResult Index() {
            ...
        }
    }
    

    因此,使用“标准”AttributeRouting(因为没有更好的名称),以下路由应该映射到您的 Index() 方法:

    • /(1)
    • /MyApp/索引 (2)

    MVC5 中新的基于属性的路由具有相似的功能(基于前者),只是语法略有不同(参见http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx

    [RoutePrefix("MyApp")]
    public class MyController : Controller {
    
        [Route("~/")] //1
        [Route("Index")] //2
        public ActionResult Index() {
            ...
        }
    }
    

    波浪号~ 似乎等同于IsAbsoluteUrl

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2021-12-29
      • 2015-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-09
      • 2018-02-01
      • 1970-01-01
      相关资源
      最近更新 更多