Question

I'm trying to setup an Area Route in my ASP.NET MVC application.

I'm also using the nuget package AttributeRouting, not the normal MVC register area routes thingy.

From my understanding, area routes look like this : /area/controller/method

What I'm trying to do is :- /api/search/index

which means:

  • Area => Api
  • Controller => SearchController
  • ActionMethod => Index

.

[RouteArea("Api")]
public class SearchController : Controller
{
    [POST("Index")]
    public JsonResult Index(IndexInputModel indexInputModel) { .. }
}

But that doesn't create that route. This is what it creates: /api/index The search controller is missing.

I've had a look the docs and noticed the RoutePrefix so I tried this..

[RouteArea("Api")]
[RoutePrefix("Search")]
public class SearchController : Controller
{
    [POST("Index")]
    public JsonResult Index(IndexInputModel indexInputModel) { .. }
}

and that actually creates the route /api/search/index.

But why do i need to put the RoutePrefix in there? Shouldn't it be smart enough to already figure out that this is a SearchController and create the 3-segment route?

Was it helpful?

Solution

You don't need to put a RoutePrefix anywhere. It's just there as a refactoring/DRY aid. Consider:

[RouteArea("Api")]
public class SearchController : Controller 
{
    [POST("Search/Index")]
    public ActionResult Index() { }
}

If you had a number of actions, maybe you want them all with the "Search" prefix, so you'd do:

[RouteArea("Api")]
[RoutePrefix("Search")]
public class SearchController : Controller 
{
    [POST("Index")]
    public ActionResult Index() { }

    // Other actions to prefix....
}

Shouldn't it be smart enough?

Not to be cheeky, but no. AR was never intended to read all your code for you and magically generate routes. It was intended to keep your URLs top of mind, and to do that you should see your URLs. Not that this is the best or only way of doing things, just that was my intent from the get.

The real reason why it isn't smart enough is that the concept of "Area" has nothing to do with URL. An area is a logical unit. You could expose that logical unit without any route prefix (so it would be hanging off ~/) or you could expose it off "This/Is/A/Prefix".

However, if you want it to be smart enough.... I just released v3.4, which will let you do this (if you want to; don't have to):

namespace Krome.Web.Areas.Api
{
    [RouteArea]
    [RoutePrefix]
    public class SearchController : Controller 
    {
        [POST]
        public ActionResult Index() { }
    }
}

This will yield the following route: ~/Api/Search/Index. The area comes from the last section of the controller's namespace; the route prefix comes from the controller name; and the rest of the url comes from the action name.

One more thing

If you want to get out a route area url and route prefix rat's nest for individual actions in a controller, do this:

[RouteArea("Api")]
[RoutePrefix("Search")]
public class SearchController : Controller 
{
    [POST("Index")]
    public ActionResult Index() { }

    [GET("Something")] // yields ~/Api/Search/Something
    [GET("NoPrefix", IgnoreRoutePrefix = true)] // yields ~/Api/NoPrefix
    [GET("NoAreaUrl", IgnoreAreaUrl = true)] // yields ~/Search/NoAreaUrl
    [GET("Absolutely-Pure", IsAbsoluteUrl = true)] // yields ~/Absolutely-Pure 
    public ActionResult Something() {}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top