nickthompson
nickthompson

Reputation: 229

Substituting ASPX file

We want to be able to swap out/substitute/override ASPX files occasionally. This is the scenario.

We have a portal written with ASP.NET which has tons of pages already - for viewing data, updating records, reports and so on. Some clients are "really important" and therefore we need to be able to customise certain pages just for them, so when they login they see a page that's customised for them.

Master pages are great - we can customise the header, footer and so on, but we might want to hide certain areas, or move them around completely. We can't do that with Master Pages.

Themes/skins are good for CSS and making controls behave differently, but again this doesn't allow me to completely reorganise a particular page.

So I want to be able to write code to go "Hey, I'm logged in as a special client, go find out if there is an 'override' .aspx page for the one I'm on. If there is, use that. Otherwise use the .aspx that's there already."

That means I can have a directory on my server for each of my "special clients" with the odd .aspx file in there that overrides the default.

How can I achieve this?

Many thanks Nick

Upvotes: 3

Views: 161

Answers (2)

Kirk Woll
Kirk Woll

Reputation: 77606

To do this, you need to register a page factory that handles .aspx files. So first create a new class that extends PageHandlerFactory:

public class MyPageFactory : PageHandlerFactory
{
    public override IHttpHandler GetHandler(
        HttpContext httpContext, string requestType, string url, 
        string pathTranslated)
    {
        // Here you can inspect `HttpContext` and perform whatever checks you
        // need to determine whether or not to use your custom overridden page.
        if (shouldOverride) 
        {
            var newVirtualPath = "/Overrides/Foo/MyPage.aspx";
            string newFilePath = httpContext.Server.MapPath(newVirtualPath);

            // Now create the page instance
            IHttpHandler page = PageParser.GetCompiledPageInstance(newVirtualPath, newFilePath, httpContext);
            return page;
        }
        else 
        {
            // If we're not overriding, just return the default implementation
            return base.GetHandler(httpContext, requestType, url, pathTranslated);
        }
    }
}

Don't forget to register it in your web.config (IIs7):

<system.webServer>
    <httpHandlers>
        <add verb="*" path="*.aspx" type="MyPageFactory" />
    </httpHandlers>
</system.webServer>

Or < IIS7:

<system.web>
    <httpHandlers>
        <add verb="*" path="*.aspx" type="MyPageFactory" />
    </httpHandlers>
</sysetm.web>

Upvotes: 3

Brian Mains
Brian Mains

Reputation: 50728

I assume the mechanism to identify the type of client to show certain information for is in the database?

Either way, to me it seems what you are talking about is providing some CMS-like functionality, where you can specialize the content on the type of user, etc. Rather than overriding the page and swapping it out, which is possible but might make it overly complex, use XML or the database to store content for certain regions of the page, and pull in those regions when the user accesses the page.

Then you can tie regions of the page to the user's role (if everything is in the database). Then you could even allow a specific role to customize the content if needed.

Upvotes: 1

Related Questions