【发布时间】:2016-03-04 15:04:47
【问题描述】:
将路线段更改为布尔值
我的控制器中有一个简单的操作方法,并且想在 url 中传递字符串“Company”时将布尔值传递给我的操作。
public ActionResult DoSomething(Boolean ? isCompany) { }
我想这样称呼它。
http://mysite/Feature/Configure/Company
所以基本上,当“公司”在 url 中时,然后将 isCompany 作为 true 布尔值传递。
这可能吗?
根据下面的答案,我认为我应该使用以下映射。
routes.MapRoute(
name: "FeatureConfigure",
url: "Feature/Configure/{company}",
defaults: new { controller = "Feature", action="Configure",
company = UrlParameter.Optional }
);
public ActionResult Configure(String company) {
var isCompany = company == "Company";
// maybe use a switch if there are multiple values company can be
// and probably rename the variable to something more generic.
}
【问题讨论】:
-
您还有其他可能匹配该路径的情况吗?即你可能有
/feature/configure/user,甚至只是/feature/configure/。 -
有两种可能?我更改了代码以反映对需要具有多个值的任何人的公司变量的更多关注。
标签: asp.net-mvc model-view-controller controller routing