开发者

Routing optional parameters with dashes in MVC

I've made an routing definition like this:

 routes.MapRoute("ProductSearch", "Search-{MainGroup}-{SubGroup}-{ItemType}",
   new {
    controller = "Product",
    action = "Search",
    MainGroup 开发者_C百科= "", SubGroup = "", ItemWebType = ""});

It is not working if the parameters are empty. Actually it resolves the url, so Url.Action method resolves the path "Search-12--" but the link is not working, so the GET of the page is not working

With slashes it is working the Url.Action method makes "Search/12"

"Search/{MainGroup}/{SubGroup}/{ItemType}"

Is it somehow possible to correct it?

I made a sample with the default mvc project: Only added: before default route:

    routes.MapRoute(DefaultSearch", "Search-{MainGroup}-{Subgroup}-{ItemType}",
        new {controller = "Home",action = "About", MainGroup = "", 
              Subgroup = "", ItemType = ""});

in Home/index.aspx:

<a href="<%=Url.Action("About", "Home", new {maingroup = "2", subgroup = "", itemType = ""}) %>">
    Search</a>

In HomeController:

public ActionResult About(string maingroup, string subgroup, string itemtype)
{
  return View();
}

Click on the link and 404


What version do you use? in mvc 2 you can use UrlParameter.Optional as default values for routes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜