Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Url.RouteUrl() with Route Names in an Area

As a side note, I understand the whole ambiguous controller names problem and have used namespacing to get my routes working, so I don't think that is an issue here.

So far I have my project level controllers and then a User Area with the following registration:

public class UserAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "User";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "UserHome",
            "User/{id}",
            new { action = "Index", controller = "Home", id = 0 },
            new { controller = @"Home", id = @"\d+" }
        );

        context.MapRoute(
            "UserDefault",
            "User/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }
}

The "UserHome" route is there so I can allow the route /User/5 vs. /User/Home/Index/5 which looks cleaner IMO.

Ideally I would like to use Url.RouteUrl("UserHome", new { id = 5 }), to generate the route elsewhere, but this always either comes back blank or gives me an exception saying it cannot find the route name, which is obviously there.

However when I use Url.RouteUrl("UserHome", new { controller = "Home", action = "Index", id = 5 }) it works no problem.

Why do I have to specify the action and controller when they have defaults already in the route mapping? What am I missing?

like image 497
ryanulit Avatar asked Jul 18 '13 21:07

ryanulit


People also ask

What is URL RouteUrl?

RouteUrl(String, Object) Generates a fully qualified URL for the specified route values by using a route name. RouteUrl(String, RouteValueDictionary) Generates a fully qualified URL for the specified route values by using a route name.

How do you add a constraint to a route?

Creating Route Constraint to a Set of Specific Values routes. MapRoute( "Default", // Route name "{controller}/{action}/{id}", // Route Pattern new { controller = "Home", action = "Index", id = UrlParameter. Optional }, // Default values for parameters new { controller = "^H.

What is the use of route name in MVC?

Route Name: A route is a URL pattern that is mapped to a handler. A handler can be a controller in the MVC application that processes the request. A route name may be used as a specific reference to a given route.

What is URL routing in MVC?

Routing enables us to define a URL pattern that maps to the request handler. This request handler can be a file or class. In ASP.NET Webform application, request handler is . aspx file, and in MVC, it is the Controller class and Action method.


2 Answers

Everyone please see @ryanulit's answer below. This issue may be fixed for you with a newer framework version.

Not sure if there has been a hot fix, but the behavior now is a bit different. Using your exact code and trying:

Url.RouteUrl("UserHome", new { id = 5 })

I now get:

/User/5?httproute=True 

This still looks awkward, so I experimented with the route and added another default param:

 context.MapRoute(
            "UserHome",
            "User/{id}",
            new { action = "Index", controller = "Home", area = "User", id = 0, 
                       httproute = true },
            new { controller = @"Home", id = @"\d+" }
        );

Now when I use

Url.RouteUrl("UserHome", new { id = 5 })

I get a nice url of

/User/5

disclaimer There could be unwanted side effects of httproute=true in the route declaration.

Also, the more verbose use:

@Url.RouteUrl("UserHome", new { controller = "Home", action = "Index", id = 5 })

still works as well.

like image 79
TSmith Avatar answered Oct 23 '22 21:10

TSmith


Try this:

@Url.Action("Index", "Home", new { Area = "User" })
like image 28
Robson Douglas Avatar answered Oct 23 '22 20:10

Robson Douglas