【发布时间】:2010-12-23 09:25:58
【问题描述】:
到目前为止,我一直在使用 ASP.NET MVC 3 BETA。一切正常,直到更新到 RC2 版本。当然我读过ScottGu's article about RC2。
我的问题如下。基本上我有 2 个控制器:
public class DynamicPageController : Controller
{
public ActionResult Redirect(string resource, int? pageNumber, int? id)
{
}
}
public class SystemController : Controller
{
public ActionResult Index()
{
}
}
在 Globals.asax 我有这样的路线:
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"SystemRoute",
"System/{action}",
new { controller = "System", action = "Index" }
);
routes.MapRoute(
"PageRoute",
"{resource}/{id}/{pageNumber}",
new { controller = "DynamicPage", action = "Redirect", resource = UrlParameter.Optional, pageNumber = UrlParameter.Optional, id = UrlParameter.Optional }
);
}
在代码中,我有简单的链接创建:
System.Web.Mvc.UrlHelper u = new System.Web.Mvc.UrlHelper(context);
string url = u.Action("Index", "System");
两个版本(BETA 和 RC2)中的 url 都是“/my_app/System”
但是下面的代码(语法同上,只是控制器和动作名称不同):
string url = u.Action("Redirect", "DynamicPage", new RouteValueDictionary(new { resource = "Home" }));
给出在 RC2 中为空的 url。它应该是(实际上在 BETA 中是)“/my_app/Home”
为什么?这是一个错误吗?如何为我的“DynamicPage”控制器创建 url?
问候
顺便说一句: 我现在可以从哪里下载 ASP.NET MVC BETA 版本以及 ASP.NET Web Pages 1.0 安装程序?自 RC2 公告以来,我在查找提到的 2 个安装程序时遇到了问题。通常我会升级我的代码,但上面描述的这个问题让我在 BETA 中停留了一段时间,因为我现在没有时间迁移和测试所有内容。
更新
当我有两个可选参数一个接一个存在时,我使用的解决方案是在现有 PageRoute 路由之前添加新的 PageRouteCore 路由:
routes.MapRoute(
"PageRouteCore",
"{resource}",
new {controller = "DynamicPage", action = "Redirect"}
);
这基本上是相同的路由,但没有可选参数。 url 创建现在的行为符合我的预期。
【问题讨论】:
标签: c# .net asp.net-mvc asp.net-mvc-3