【发布时间】:2014-06-28 03:21:26
【问题描述】:
最近我参加面试,他在问mvc中的动态路由。
问题是如何根据参数字符串或int动态路由某些动作方法。
例如:
Public ActionResult Add(datatype variable)
{
//depending upon the Value he was asking how to redirect.
}
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-4
最近我参加面试,他在问mvc中的动态路由。
问题是如何根据参数字符串或int动态路由某些动作方法。
例如:
Public ActionResult Add(datatype variable)
{
//depending upon the Value he was asking how to redirect.
}
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-4
重定向不由路由处理,它由执行的操作处理。
我想他的意思是如果数据类型是string,你如何创建路由规则来执行动作Foo,如果数据类型是int,你如何执行动作Bar。
您使用Routing Constraints,下面我添加了所有支持的约束的屏幕截图。
这就是你将如何使用它们
public class HomeController: Controller
{
// foobar.com/home/123
[Route("home/{id:int}")]
public ActionResult Foo(int id)
{
return this.View();
}
// foobar.com/home/onetwothree
[Route("home/{id:alpha}")]
public ActionResult Bar(string id)
{
return this.View();
}
}
更多信息可以在here找到。
【讨论】: