简体   繁体   中英

How to convert this fix this ASP.NET MVC5 route?

I'm not sure how to convert this AttributeRoute into an MVC5 route.

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

The IsAbsoluteUrl is one of the things that is confusing me.

Based on the notes found here: http://attributerouting.net/#route-prefixes the IsAbsoluteUrl flag is designed to ignore the RoutePrefix defined on the Controller. For example:

[RoutePrefix("MyApp")]
public class MyController : Controller {

    [GET("", IsAbsoluteUrl = true)] //1
    [GET("Index")] //2
    public ActionResult Index() {
        ...
    }
}

So, using the 'standard' AttributeRouting (for lack of a better name), the following routes should map to your Index() method:

  • / (1)
  • /MyApp/Index (2)

The new Attribute based routing in MVC5 has similar functionality (being based on the former), just slightly different syntax (see 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() {
        ...
    }
}

The tilde ~ seems to be equivalent of IsAbsoluteUrl .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM