代ASPX文件 [英] Substituting ASPX file

查看:98
本文介绍了代ASPX文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们希望能够换出/替代/覆盖ASPX文件偶尔。这是该方案。

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

我们与ASP.NET有吨的网页已经写了门户网站 - 用于查看数据,更新记录,报告等。有些客户是非常重要的,因此,我们需要能够自定义某些网页只是为了他们,所以当他们登录他们看到的定制他们的页面。

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.

主题/皮肤是良好的CSS,使控制行为不同,但同样,这并不让我完全重组一个特定的页面。

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

所以我想能写code去嘿,我登录的一个特殊的客户端,去找出是否有一个覆盖了一个我在.aspx页面中如果有,则使用。否则,使用的存在已经在.aspx。

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."

这意味着我可以有我与奇.aspx文件服务器为每个我的特殊客户对一个目录有覆盖缺省。

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

推荐答案

要做到这一点,你需要注册一个页面工厂处理的.aspx 文件。因此,首先创建一个扩展的新类 PageHandlerFactory

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

不要忘了在你的 web.config中注册它(IIS7):

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

或LT; IIS7:

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

这篇关于代ASPX文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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