开发者

Routing optional parameters with dashes in MVC

开发者 https://www.devze.com 2022-12-31 15:50 出处:网络
I\'ve made an routing definition like this: routes.MapRoute(\"ProductSearch\", \"Search-{MainGroup}-{SubGroup}-{ItemType}\",

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

精彩评论

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