如何更改routes.MapRoute的MVC3的顺序? [英] How do I change the order of routes.MapRoute in MVC3?

查看:395
本文介绍了如何更改routes.MapRoute的MVC3的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在注册的global.asax.cs路由时更改URL参数的顺序?

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

我注册的路线,像这样:(注:我也有注册,以及默认的MVC3路线)

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" });

我的控制器:

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

当我键入网址... /调查/ 1 / AddQuestions资源不能被发现。当我运行的路线调试器的路径显示为有效的。

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.

这在MVC3甚至有可能?我知道,在宁静的WCF,你可以构建像这样没有问题的路线。在事物的宏伟计划我大概可以用{控制器} / {行动} / {ID}生活,但我相信使用动词只在必要时,在我的情况下,正确的URL应该按我上面的例子来构成的。

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.

任何想法?谢谢你。

推荐答案

您的路线缺少一个默认的控制器,如:

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...

这篇关于如何更改routes.MapRoute的MVC3的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆