Question

i've just started using AttributeRouting with my ASP.NET MVC3 application. I started out with -no- controllers at all. (New Empty MVC3 Application)

I then made an area. (called: Documentation)

I then added a controller (called: DocumentationController)

I've then done this..

[RouteArea("Documentation")]
public class DocumentationController : Controller
{
    [GET("Index")]
    public ActionResult Index()
    {
        return View();
    }
}

And the following route, works: /documentation/index

but how can I make these two routes, work?

1 - / <-- (default route / no specific route specified) 2 - /documentation <-- no 'index' subroute section added.

Can this be done with AttributeRouting?

UPDATE:

I know how to do this with the default ASP.NET MVC3 structure, etc. What I'm trying to do is figure this out via AttributeRouting instead.

Was it helpful?

Solution

I assume you want the "/" and "/documentation" to map to DocumentationController.Index, yes? If so, do this:

[RouteArea("Documentation")]
public class DocumentationController : Controller
{
    [GET("Index", Order = 1)] // will handle "/documentation/index"
    [GET("")] // will handle "/documentation"
    [GET("", IsAbsoluteUrl = true)] // will handle "/"
    public ActionResult Index()
    {
        return View();
    }
}

A bit of explanation:

  • GET("Index") has Order = 1 to mark it as the primary route for the action. Due to how reflection works, there is no way to determine the order of attributes on an action without using the Order property. See here
  • You can have multiple get routes mapped to a single action. See here
  • The IsAbsoluteUrl property allows you to override the URL prefixes added by the RouteArea and RoutePrefix attributes. So that final route will match root requests. See here

Hope this helps. If my initial assumption of what you're trying to do is incorrect, please comment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top