我可以使用HttpHandler来伪造aspx页面的存在吗?

3

我正在使用ASP.NET 3.5构建一个网站,大部分的站点结构静态化程度足以创建文件夹结构和aspx页面。然而,网站管理员希望通过Web界面和使用所见即所得编辑器向网站的不同部分添加新页面。我正在使用嵌套的母版页为网站的不同部分提供自己的菜单。我想做的是在每个站点的通用页面下使用适当的母版页,并具有可以从数据库加载内容的占位符。 我还希望这些“虚假”页面具有与任何其他aspx页面相同的URL,就像它们在服务器上有相应的文件一样。 因此,我的URL不应该是:

http://mysite.com/subsection/gerenicconent.aspx?contentid=1234

这句话的意思是:这将会是类似于以下内容:
http://mysite.com/subsection/somethingmeaningful.aspx

问题在于somethingmeaningful.aspx不存在,因为管理员是通过Web UI创建它,而内容存储在数据库中。我的想法是实现一个HTTP处理程序来处理aspx文件的请求。在该处理程序中,我将检查所请求的URL是否是实际文件或者是我“伪造的页面”之一。如果是对虚假页面的请求,我将重新路由请求到适当部分的通用内容页面,更改查询字符串以从数据库请求适当的数据,并重写URL,使用户看起来伪造的页面真的存在。目前我遇到的问题是我无法弄清楚如何将请求路由到默认的aspx页面处理程序。我尝试实例化一个PageHandlerFactory,但构造函数是受保护的内部构造函数。有没有办法告诉我的HttpHandler调用通常用于处理请求的HttpHandler?我的处理程序代码目前看起来像这样:
using System.Web;
using System.Web.UI;

namespace HandlerTest
{
    public class FakePageHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            if(RequestIsForFakedPage(context))
            {
                // reroute the request to the generic page and rewrite the URL
                PageHandlerFactory factory = new PageHandlerFactory(); // this won't compile because the constructor is protected internal
                factory.GetHandler(context, context.Request.RequestType, GetGenericContentPath(context), GetPhysicalApplicationPath(context)).ProcessRequest(context);
            }
            else
            {
                // route the request to the default handler for aspx pages
                PageHandlerFactory factory = new PageHandlerFactory();
                factory.GetHandler(context, context.Request.RequestType, context.Request.Path, context.Request.PhysicalPath).ProcessRequest(context);
            }
        }

        public string RequestForPageIsFaked(HttpContext context)
        {
            // TODO
        }

        public string GetGenericContentPath(HttpContext context)
        {
            // TODO
        }

        public string GetPhysicalApplicationPath(HttpContext context)
        {
            // TODO
        }
    }
}

我还需要做一些工作来确定请求是否是一个真实的页面,我还没有重写任何URL,但是这种方法可行吗?除了调用构造函数之外,还有其他创建PageHandlerFactory的方法吗?有没有办法将请求路由到aspx页面的“正常”HttpHandler?基本上我会说“按照你通常处理ASPX请求的方式来处理它。”

3个回答

3

2

1

我刚从我们刚写的类似系统中提取了这个。

这种方法处理物理页面和“虚假”页面。我相信你能确定它如何适配你的虚假页面架构。

public class AspxHttpHandler : IHttpHandlerFactory
{
        #region ~ from IHttpHandlerFactory ~

        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
                    string url=context.Request.Url.AbsolutePath;
            string[] portions = url.Split(new char[] { '/', '\\' });
                    // gives you the path, i presume this will help you identify the section and page
                    string serverSidePage=Path.Combine(context.Server.MapPath("~"),url);
                    if (File.Exists(serverSidePage))
                    {
                             // page is real
                            string virtualPath = context.Request.Url.AbsolutePath;
                string inputFile = context.Server.MapPath(virtualPath);

                try
                {
                                    // if it's real, send in the details to the ASPX compiler
                    return PageParser.GetCompiledPageInstance(virtualPath, inputFile, context);
                }
                catch (Exception ex)
                {
                        throw new ApplicationException("Failed to render physical page", ex);
                }
                      }
                      else
                      {
                            // page is fake
                            // need to identify a page that exists which you can use to compile against
                            // here, it is CMSTaregtPage - it can use a Master
                            string inputFile = context.Server.MapPath("~/CMSTargetPage.aspx");
                string virtualPath = "~/CMSTargetPage.aspx";
                            // you can also add things that the page can access vai the Context.Items collection
                            context.Items.Add("DataItem","123");
                            return PageParser.GetCompiledPageInstance(virtualPath, inputFile, context);
}

public void ReleaseHandler(IHttpHandler handler)
{

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接