首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >参数为空的.NET MVC自定义路由

参数为空的.NET MVC自定义路由
EN

Stack Overflow用户
提问于 2009-10-29 06:50:40
回答 1查看 1.2K关注 0票数 0

我有一个包含以下路由的.net mvc:

代码语言:javascript
复制
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 main

此请求不工作:

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

并不是所有的请求都有参数,我希望将空参数作为空字符串或null传递给方法。

我哪里错了?

方法:

代码语言:javascript
复制
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);
    }
EN

回答 1

Stack Overflow用户

发布于 2009-10-29 07:31:27

我发现一个问题,您的第二个和第三个路由具有完全相同的URL参数。所以第三个路由永远不会被调用。你怎么会把它放在那儿?看起来你可以简单地删除第二条路线。

而且,第二路由比第一路由具有更少的参数。这意味着第一个路由可能与您发布的两个URL都匹配。您可能应该对这些路由重新排序。

更新:哦!我没有注意到URL中的双斜杠。那是行不通的。就ASP.NET而言,它不是有效的URL,因此ASP.NET甚至在请求到达路由之前就阻止了它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1640648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档