How do I change the order of routes.MapRoute in MVC3?

无人久伴 提交于 2020-01-03 02:54:07

问题


How do I change the order of the url parameter when registering routes in global.asax.cs?

I registered the route like so: (Note: I also have the default MVC3 route registered as well)

routes.MapRoute(
  "SurveyWizard",
  "survey/{id}/{action}",
  new { id = UrlParameter.Optional, action = "AddQuestions" });

My controller:

public ActionResult AddQuestions(int id)
{
  if(id < 1 || id == null)
    //Redirect somewhere  
  var survey = _surveyService.GetSurveyById(id);
  //other controller logic
  return View(survey);
}

When I type the url .../survey/1/AddQuestions the resource can't be found. When I run a route debugger the route shows as valid.

Is this even possible in MVC3? I know in Restful WCF, you can structure routes like this no problem. In the grand scheme of things I could probably live with {controller}/{action}/{id}, but I believe in using verbs only when necessary and in my case the correct url should be structured per my example above.

Any ideas? Thanks.


回答1:


Your route is missing a default controller, like:

routes.MapRoute(
  "SurveyWizard",
  "survey/{id}/{action}",
  new { controller="Survey", action="AddQuestions" });

Another point you should be aware of is that the default rote should be the last one...



来源:https://stackoverflow.com/questions/5542345/how-do-i-change-the-order-of-routes-maproute-in-mvc3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!