2009-10-28 63 views
0

我有以下途徑一個.NET MVC .NET MVC自定義路由:空參數

routes.Add(new Route(
      "Lookups/{searchtype}/{inputtype}/{firstname}/{middlename}/{lastname}/{city}/{state}/{address}", 
      new RouteValueDictionary(new { controller = "Lookups", action = "Search", firstname = (string)null, middlename = (string)null, lastname = (string)null, city = (string)null, state = (string)null, address = (string)null, SearchType = SearchType.PeopleSearch, InputType = InputType.Name }), 
      new MvcRouteHandler()) 
     ); 

     routes.Add(new Route(
      "Lookups/{searchtype}/{inputtype}", 
      new RouteValueDictionary(new { controller = "Lookups", action = "Search", firstname = "", middlename = "", lastname = "", city = "", state = "", address = "" }), 
      new MvcRouteHandler()) 
     ); 

     routes.Add(new Route(
      "Lookups/{searchtype}/{inputtype}", 
      new RouteValueDictionary(new { controller = "Lookups", action = "Search", firstname = "", middlename = "", lastname = "", city = "", state = "", address = "", SearchType = SearchType.PeopleSearch, InputType = InputType.Name }), 
      new MvcRouteHandler()) 
     ); 

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

以下要求正常工作:

http://localhost:2608/Lookups/PeopleSearch/Name/john/w/smith/seattle/wa/123主要

這個請求不起作用:

http://localhost:2608/Lookups/PeopleSearch/Name/john//smith//wa/

不是所有的請求都會有所有參數,我希望空參數作爲空字符串或null傳遞給方法。

我哪裏錯了?

的方法:

public ActionResult Search(string firstname, string middlename, string lastname, string city, string state, string address, SearchType searchtype, InputType inputtype) 
    { 
     SearchRequest r = new SearchRequest { Firstname = firstname, Middlename = middlename, Lastname = lastname, City = city, State = state, Address = address, SearchType = searchtype, InputType = inputtype }; 
     return View(r); 
    } 
+2

如果你還沒有使用它 - http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx – 2009-10-28 23:00:50

+0

非常有幫助。謝謝 – user135498 2009-10-28 23:05:31

+0

該工具非常棒,但它無法調試我的問題。 – user135498 2009-10-28 23:15:42

回答

2

我看到一個問題,你的第二個和第三個路徑具有完全相同的URL參數。所以第三條路線永遠不會被調用。你爲什麼在那裏?看起來你可以簡單地刪除第二條路線。

此外,第二條路線的參數比第一條路線少。這意味着第一條路線可能會匹配您發佈的兩個網址。您應該重新排列這些路線。

更新:哦!我沒有注意到URL中的雙斜線。那永遠不會工作。就ASP.NET而言,它不是一個有效的URL,因此即使在請求路由之前,ASP.NET也會阻止該請求。

+0

以簡單的名義,我刪除了第二條和第三條路線。同樣的結果。 – user135498 2009-10-29 00:02:24

+0

我現在要使用標準參數。當我不在最後期限之後,我會在稍後時間製作網址。 – user135498 2009-10-29 04:20:15