-->

MVC 2.0 - different view based on URL with shared

2019-07-27 01:21发布

问题:

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.