我想在asp.net mvc中执行以下操作:
myquizsite.com/quizzes - list quizzes available
myquizsite.com/quizzes/quiz1 - list previous instances of quiz1
myquizsite.com/quizzes/quiz2 - list previous instances of quiz2
myquizsite.com/quizzes/quiz1/create - create new quiz1
使用默认路由,这意味着"创建"参数被传递到动作quiz1以创建它的新实例。
这对我来说似乎很奇怪,所以我猜测有更好的方法,但我不确定如何。
答案 0 :(得分:3)
使用MVC属性路由
public QuizzesController :Controller
{
[Route("quizzes/{id?}")]
public ActionResult Index(string id="")
{
if(!String.IsNullOrEmpty(id))
{
//to do :Return quizzes available
}
else
{
//To do :Return previous instances of id passed(Ex :quiz1/quiz2)
}
}
[Route("quizzes/{id}/create")]
public ActionResult Create(string id)
{
// to do : return Create view for Id passed (Ex :quiz1/quiz2)
}
}
您可以在RouteConfig.cs文件中的RegisterRoutes
方法中启用属性路由。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes(); //This line enables attribute routing
//Default Route definition goes here
}