与多视图单控制器 [英] Single Controller with Multiple Views

查看:102
本文介绍了与多视图单控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建具有多个视图MVC应用程序,但使用一个控制器。我开始通过创建,我可以用它来重定向到第二个文件夹另一个属性第二个路径。

 公共静态无效的RegisterRoutes(RouteCollection路线)
    {
        routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);        routes.MapRoute(
            XML,//路线名称
            XML / {控制器} / {行动} / {ID},// URL带参数
            新{模式=XML,控制器=家,行动=索引,ID =} //参数默认
        );        routes.MapRoute(
            默认,//路线名称
            {控制器} / {行动} / {ID},// URL带参数
            新{控制器=家,行动=索引,ID =} //参数默认
        );
    }
    保护无效的Application_Start()
    {
        的RegisterRoutes(RouteTable.Routes);
        SessionManager.Instance.InitSessionFactory(acstech.helpWanted);
        ViewEngines.Engines.Clear();
        ViewEngines.Engines.Add(新ModeViewEngine());
    }

我也跟着通过从WebFormViewEngine下降和〜/查看到〜/ {}模式改变视图的路径。这个工作,跑了正确呈现的页面。我跑进问题是Html.ActionLink总是使用模式版本不管视图中呈现的东西。这是实现我的目标,如果是这样我缺少什么让操作链接正常工作正确的方向。下面是视图引擎。这是实验室的测试,因此已经采取了一些自由。

 公共类ModeViewEngine:WebFormViewEngine
{
    公共ModeViewEngine()
    {    }    保护覆盖IVIEW CreatePartialView(ControllerContext controllerContext,串partialPath)
    {
        字符串模式=的String.Empty;
        如果(controllerContext.RouteData.Values​​ [模式]!= NULL)
            模式= controllerContext.RouteData.Values​​ [模式]作为字符串;
        返回新WebFormView(partialPath.Replace(〜/浏览次数,〜/+模式+意见),NULL);
    }    保护覆盖IVIEW CreateView的(ControllerContext controllerContext,串viewPath,串masterPath)
    {
        字符串模式=的String.Empty;
        如果(controllerContext.RouteData.Values​​ [模式]!= NULL)
            模式= controllerContext.RouteData.Values​​ [模式]作为字符串;
        返回新WebFormView(viewPath.Replace(〜/浏览次数,〜/+模式+意见),masterPath);
    }
}


解决方案

你有没有尝试添加模式=中的默认路由的默认值数组中?也就是说,按照我的理解,如何索引的行动被从URL中忽略,因此在理论上应使其符合您的默认路由我相信。

I am trying to create an MVC application with multiple view, but using a single controller. I started by creating a second route with another property that I could use to redirect to a second folder.

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

I followed that by descending from WebFormViewEngine and changed the path from ~/View to ~/{mode}View. This worked and ran rendered the pages properly. The problem that I ran into is that the Html.ActionLink always uses the mode version no matter what the view rendered. Is this the proper direction for accomplishing my goal, if so what am I missing to get the Action Link to work properly. Below is the ViewEngine. This is a lab test, so some liberties have been taken.

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

解决方案

Have you tried adding mode="" in the defaults array in the default route? That is, as I understand it, how the "Index" action gets omitted from URLs, so that should in theory make it match your default route I believe.

这篇关于与多视图单控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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