Hi How do I map the url ../Companies/Results/value/id when both the parameters are optional?
嗨如果两个参数都是可选的,我如何映射网址../Companies/Results/value/id?
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
公司是控制器,结果是动作,值和id是可选参数。在我的表单上是一个值的文本框和一个id的选择列表。用户可以选择两个或每个中的一个来搜索。尝试过这样的事情,但是当其中一个可选参数(例如值)丢失时无法处理,例如../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
);
3 个解决方案
#1
8
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.
您不能拥有包含两个可选参数的路由,由于您描述的问题,只有最后一个参数可以精确选择。我建议你有一个值的默认参数,比如byid,并在人选择职业时使用它。
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.
我假设您正在通过javascript构建URL,因为使用GET表单操作会导致参数名称被添加到URL。在这种情况下,当文本框为空时,只需插入默认的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.
更新您的路由以包含默认值,以便您生成的任何网址都可以使用。有关处理生成具有两个“可选”参数的URL的替代方法,请参阅Phil Haack关于此的博客文章。
// 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
);
#2
1
Thanks guys, just discovered route constraints for integer. And so fiddling around with some combinations of routes it seems to work the way I want :
谢谢大家,刚刚发现整数的路由约束。所以摆弄一些路线组合似乎按照我想要的方式工作:
routes.MapRoute(
"Detail", // Route name
"{action}/{value}", // URL with parameters
new { controller = "Companies", action = "Detail" }, // Parameter defaults
new { value = @"\d+" } //integer only
);
routes.MapRoute(
"Company + Profession", // Route name
"{action}/{value}/{profId}", // URL with parameters
new { controller = "Companies", action = "Results" }, // Parameter defaults
new { profId = @"\d+" } //integer only
);
routes.MapRoute(
"Profession", // Route name
"{action}/{profId}", // URL with parameters
new { controller = "Companies", action = "Results"}, // Parameter defaults
new {profId = @"\d+" } //integer only
);
routes.MapRoute(
"Company", // Route name
"{action}/{value}", // URL with parameters
new { controller = "Companies", action = "Results" } // Parameter defaults
);
routes.MapRoute(
"RootFolder", // Route name
"{action}/{value}", // URL with parameters
new { controller = "Companies", action = "Index", value = UrlParameter.Optional } // Parameter defaults
);
#3
0
I'm not sure, since I don't have now where to try this, but here is my suggestion
我不确定,因为我现在没有在哪里尝试这个,但这是我的建议
routes.MapRoute(
"Company+Profession", // Route name
"Companies/{action}/value-{value}/id-{profId}", // URL with parameters
new { controller = "Companies", action = "Index", value = UrlParameter.Optional, profId = UrlParameter.Optional } // Parameter defaults
);
#1
8
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.
您不能拥有包含两个可选参数的路由,由于您描述的问题,只有最后一个参数可以精确选择。我建议你有一个值的默认参数,比如byid,并在人选择职业时使用它。
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.
我假设您正在通过javascript构建URL,因为使用GET表单操作会导致参数名称被添加到URL。在这种情况下,当文本框为空时,只需插入默认的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.
更新您的路由以包含默认值,以便您生成的任何网址都可以使用。有关处理生成具有两个“可选”参数的URL的替代方法,请参阅Phil Haack关于此的博客文章。
// 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
);
#2
1
Thanks guys, just discovered route constraints for integer. And so fiddling around with some combinations of routes it seems to work the way I want :
谢谢大家,刚刚发现整数的路由约束。所以摆弄一些路线组合似乎按照我想要的方式工作:
routes.MapRoute(
"Detail", // Route name
"{action}/{value}", // URL with parameters
new { controller = "Companies", action = "Detail" }, // Parameter defaults
new { value = @"\d+" } //integer only
);
routes.MapRoute(
"Company + Profession", // Route name
"{action}/{value}/{profId}", // URL with parameters
new { controller = "Companies", action = "Results" }, // Parameter defaults
new { profId = @"\d+" } //integer only
);
routes.MapRoute(
"Profession", // Route name
"{action}/{profId}", // URL with parameters
new { controller = "Companies", action = "Results"}, // Parameter defaults
new {profId = @"\d+" } //integer only
);
routes.MapRoute(
"Company", // Route name
"{action}/{value}", // URL with parameters
new { controller = "Companies", action = "Results" } // Parameter defaults
);
routes.MapRoute(
"RootFolder", // Route name
"{action}/{value}", // URL with parameters
new { controller = "Companies", action = "Index", value = UrlParameter.Optional } // Parameter defaults
);
#3
0
I'm not sure, since I don't have now where to try this, but here is my suggestion
我不确定,因为我现在没有在哪里尝试这个,但这是我的建议
routes.MapRoute(
"Company+Profession", // Route name
"Companies/{action}/value-{value}/id-{profId}", // URL with parameters
new { controller = "Companies", action = "Index", value = UrlParameter.Optional, profId = UrlParameter.Optional } // Parameter defaults
);