【发布时间】:2009-03-05 18:48:31
【问题描述】:
我正在尝试创建具有多个视图但使用单个控制器的 MVC 应用程序。我首先使用另一个属性创建第二条路由,我可以使用它来重定向到第二个文件夹。
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"xml", // Route name
"xml/{controller}/{action}/{id}", // URL with parameters
new { mode = "xml", controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
SessionManager.Instance.InitSessionFactory("acstech.helpWanted");
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new ModeViewEngine());
}
我随后从 WebFormViewEngine 下降并将路径从 ~/View 更改为 ~/{mode}View。这工作并运行正确地呈现了页面。我遇到的问题是 Html.ActionLink 始终使用模式版本,无论视图呈现什么。这是实现我的目标的正确方向吗?如果是这样,我缺少什么来让动作链接正常工作。下面是 ViewEngine。这是一个实验室测试,所以有些冒昧。
public class ModeViewEngine : WebFormViewEngine
{
public ModeViewEngine()
{
}
protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
{
string mode = String.Empty;
if (controllerContext.RouteData.Values["mode"] != null)
mode = controllerContext.RouteData.Values["mode"] as string;
return new WebFormView(partialPath.Replace("~/Views", "~/" + mode + "Views"), null);
}
protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
{
string mode = String.Empty;
if (controllerContext.RouteData.Values["mode"] != null)
mode = controllerContext.RouteData.Values["mode"] as string;
return new WebFormView(viewPath.Replace("~/Views", "~/" + mode + "Views"), masterPath);
}
}
【问题讨论】:
标签: asp.net-mvc model-view-controller