带有空参数的.NET MVC自定义路由 [英] .NET MVC custom routing with empty parameters

查看:146
本文介绍了带有空参数的.NET MVC自定义路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.net mvc,它具有以下路由:

I have a .net mvc with the following routes:

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
        );

以下请求可以正常工作:

The following request works fine:

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

此请求无效:

http://localhost:2608/Lookups/PeopleSearch/名称/john//smith//wa/

并非所有请求都具有所有参数,我希望将空参数作为空字符串或null传递给该方法.

Not all requests will have all paramters and I would like empty parameters to be passed to the method as empty string or 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);
    }

推荐答案

我看到一个问题,您的第二条和第三条路线具有完全相同的URL参数.因此,第三条路线将永远不会被呼叫.你为什么在那里呢?看来您可以简单地删除第二条路线.

I see one problem, your second and third route have exactly the same URL parameters. So the third route will never get called. Why do you have that there? It looks like you could simply delete the second route.

此外,第二条路线的参数少于第一条路线.这意味着第一个路由可能会与您发布的两个URL都匹配.您可能应该重新排序这些路线.

Also, the second route has less parameters than the first route. That means the first route will probably match both the URLs that you posted. You should probably re-order those routes.

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

UPDATE: Oh! I didn't notice the double slash in the URL. That'll never work. It's not a valid URL as far as ASP.NET is concerned and thus ASP.NET is blocking the request even before it gets to Routing.

这篇关于带有空参数的.NET MVC自定义路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆