开发者

ASP.NET MVC 3 RC 2 Routing problem

I changed the default routing in ASP.NET MVC from

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

to

        routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}/{lineNo}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional, lineNo = UrlParameter.Optional });

but now all @Html.ActionLink() calls are rendered to href="". If I change the route back to default all links are working again.

I used the same route with RC1 and it worked perfectly.

I didn't find anything in the release docs so I think I'm d开发者_如何学Gooing it wrong.

Regards, Steffen


In a route an optional parameter can appear only at the end. This means that in your route definition the id parameter cannot be optional. You need to explicitly set it to a value.

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}/{lineNo}",
    new { 
        controller = "Home", 
        action = "Index", 
        lineNo = UrlParameter.Optional 
    }
);

And when you generate a link you must always provide a value for the id parameter if you want this route to match:

@Html.ActionLink("some link", "index", new { id = "123" })

As an alternative you might give a default value to the id parameter:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}/{lineNo}",
    new { 
        controller = "Home", 
        action = "Index", 
        id = "123",
        lineNo = UrlParameter.Optional 
    }
);

Now you no longer need to specify it in your links.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜