Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

asp.net mvc 4 - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...