ASP MVC路由与两个可选参数 [英] asp mvc routing with two optional parameters

查看:112
本文介绍了ASP MVC路由与两个可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我如何对应网址的 ../公司/业绩/价值/ ID 当两个参数是可选的?

公司是控制器,结果是行动,价值和id是可选参数。在我的形式是价值一个文本框和一个ID的选择列表。用户可以同时选择两个或各一个的通过进行搜索。尝试过这样的事情,但不能处理时,可选参数之一,说值,缺少如../Companies/Results/ / ID

  routes.MapRoute(
            公司+专业,//路线名称
            与参数{行动} / {值} / {} PROFID,// URL
            新{控制器=公司,行动=索引,值= UrlParameter.Optional,PROFID = UrlParameter.Optional} //参数默认
        );        routes.MapRoute(
            默认,//路线名称
            {控制器} / {行动} / {ID},// URL带参数
            新{控制器=家,行动=索引,ID = UrlParameter.Optional} //参数默认
        );


解决方案

您不能有两个可选参数的路线,只有最后一个参数可以是可选的precisely,因为你描述的问题。我建议你​​有,例如 byid 默认的参数并使用这个时候的人选择的职业。

我假设你通过JavaScript构造URL,因为使用GET形式的行动将导致参数名称被添加到URL。在这种情况下,当文本框为空,只需将默认的 byid

更新你的路线,包括默认,让你产生任何的URL会工作。见菲尔哈克的<一个href=\"http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx\">blog帖子在这一种替代的方法来处理产生具有两个可选的参数的URL。

  //当同时指定使用的参数
routes.MapRoute(
        公司+专业,//路线名称
        与参数{行动} / {值} / {} PROFID,// URL
        新{控制器=公司,行动=索引,值=byid,PROFID = UrlParameter.Optional} //参数默认
);

Hi How do I map the url ../Companies/Results/value/id when both the parameters are optional?

Companies is the controller, Results is the action, value and id are optional parameters. On my form is a textbox for value and a selectlist for an id. The user could select both or one of each to search by. Tried something like this but cant handle when one of the optional parameters, say value, is missing such as ../Companies/Results/ /id

        routes.MapRoute(
            "Company+Profession", // Route name
            "{action}/{value}/{profId}", // URL with parameters
            new { controller = "Companies", action = "Index", value = UrlParameter.Optional, profId = UrlParameter.Optional } // Parameter defaults
        );

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

解决方案

You can't have a route that has two optional parameters, only the last parameter can be optional precisely because of the problem you describe. I suggest that you have a default parameter for value, like byid and use this when the person selects a profession.

I assume that you're constructing the URL via javascript, since using a GET form action would result in the parameter names being added to the URL. In this case, when the textbox is empty simply insert the default byid.

Update your route to include the default so any URLs that you generate will work. See Phil Haack's blog post on this for an alternative way to handle generating URLs that have two "optional" parameters.

// used when both parameters are specified
routes.MapRoute(
        "Company+Profession", // Route name
        "{action}/{value}/{profId}", // URL with parameters
        new { controller = "Companies", action = "Index", value ="byid", profId = UrlParameter.Optional } // Parameter defaults
);

这篇关于ASP MVC路由与两个可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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