MVC 2.0 - different view based on URL with shared controls

烂漫一生 提交于 2019-12-24 18:43:23

问题


I have 2 master pages. One is intended to be shown in a normal standalone website. The other is to be used in external sites as an Iframe.

I want to be able to show the normal page at http://example.com/home/index and the iframed version at http://example.com/framed/home/index

I want to have controls that will postback to one controller so I don't have to duplicate logic, so they must be available in both the normal and iframed versions.

My problem is that when I try and use areas, I just can't get them to work right with the default url. Also, I have the added complication of structuremap. When I try and hit /area/controller/action, I get

The IControllerFactory 'MySite.Web.Code.IoC.StructureMapControllerFactory' did not return a controller for the name 'MyArea'.

Does anyone know how to make this kind of setup work? Really all I'm doing is trying to show one set of views if it has /Framed/controller/action and another set if it does not have /framed. I thought areas were the way to go, but maybe not.


回答1:


All of our controllers implement the same base class, and we use the following override to do what you're describing:

protected override ViewResult View(string viewName, string masterName, object model)
{
    if (masterName == null)
    {
        var options = PortalRequestManager.CurrentPortalRouteOptions;
        masterName = options.MvcMasterPath;
    }
    return base.View(viewName, masterName, model);
}

All of our AreaRegistrations use the following method to register their areas:

    public static void RegisterMvcAreaRoutes(AreaRegistrationContext context, string name, string url,
                                             object defaults)
    {
        context.MapRoute(name + "Portal",
                         "P/Channel/" + url,
                         defaults);
        context.MapRoute(name + "FramePortal",
                         "F/Channel/" + url,
                         defaults);
    }

And then the PortalRequestManager that you saw in the first code block parses the URL to see if it uses "/P" or "/F" to determine which MvcMasterPath to use.

We use Ninject's controller factory, which has no problem with this setup, so I can't really speak to your problems with StructureMap.



来源:https://stackoverflow.com/questions/3936702/mvc-2-0-different-view-based-on-url-with-shared-controls

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