本文介绍了自PageHandlerFactory为的.aspx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个相当简单的CMS。我需要截取为广大在我的Web应用程序中的.aspx页面的请求,以获得对输出的完全控制。在大多数情况下,输出将来自高速缓存被拉出并且将只是纯HTML

I am building a fairly simple CMS. I need to intercept requests for the majority of .aspx pages in my web application, in order to gain complete control over the output. In most cases, the output will be pulled from cache and will just be plain HTML.

不过,仍有一对夫妇的,我将需要使用ASP页:上的控件。我想对我来说,绕过几个特定的​​请求将继承System.Web.UI.PageHandlerFactory并调用MyBase实现的时候,我需要(请纠正我,如果我错了这里)的最佳方式。但我怎么所有其他请求传递到我的自定义处理程序?

However, there still are a couple of pages that I am going to need to use asp: controls on. I assume the best way for me to bypass a few particular requests would be to inherit System.Web.UI.PageHandlerFactory and just call the MyBase implementation when I need to (please correct me if I am wrong here). But how do I transfer all other requests to my custom handler?

推荐答案

当我写了一个简单的CMS,我使用PageHandlerFactory得到它做什么,我想有困难的时候。最后我切换到IHttpModule的。

When I wrote a simple CMS, I had a difficult time using the PageHandlerFactory to get it to do what I wanted. In the end I switched to a IHttpModule.

我的模块将首先检查,看看是否有在请求路径.aspx文件。我只能做,如果页面上有用户控件或者没有融入CMS出于某种原因。因此,如果该文件存在,它会返回到模块之外。之后,它会看所请求的路径和它凝结成一个导航标记。因此〜/公司简介/ Default.aspx的将成为page.aspx?NT = aboutusdefault。 page.aspx将加载适当的内容构成了CMS。当然,重定向会出现服务器端让用户/蜘蛛永远不知道什么不同的事。

My module would first check to see if there was an .aspx file in the requested path. I'd only do that if the page has user controls on it or didn't fit into the CMS for some reason. So if the file existed, it would return out of the module. After that it would look at the requested path and condense it into a "navigation tag." Thus ~/aboutus/default.aspx would become page.aspx?nt=aboutusdefault. page.aspx would load the proper content form the CMS. Of course, the redirect occurs server-side so the users/spiders never know anything different happened.

using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web;

namespace MyCMS.Handlers {
    /// <summary>
    /// Checks to see if we should display a virutal page to replace the current request.
    /// Code adapted from:
    /// Rewrite.NET -- A URL Rewriting Engine for .NET
    /// By Robert Chartier
    /// http://www.15seconds.com/issue/030522.htm
    /// </summary>
    public class VirtualPageModule : IHttpModule {
        /// <summary>
        /// Init is required from the IHttpModule interface
        /// </summary>
        /// <param name="Appl"></param>
        public void Init(System.Web.HttpApplication Appl) {
            // make sure to wire up to BeginRequest
            Appl.BeginRequest += new System.EventHandler(Rewrite_BeginRequest);
        }

        /// <summary>
        /// Dispose is required from the IHttpModule interface
        /// </summary>
        public void Dispose() {
            // make sure you clean up after yourself
        }

        /// <summary>
        /// To handle the starting of the incoming request
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public void Rewrite_BeginRequest(object sender, System.EventArgs args) {
            // Cast the sender to an HttpApplication object
            HttpApplication httpApp = (HttpApplication)sender;

            // See if the requested file already exists
            if (System.IO.File.Exists(httpApp.Request.PhysicalPath)) {
                // Do nothing, process the request as usual
                return;
            }

            string requestPath = VirtualPathUtility.ToAppRelative(httpApp.Request.Path);

            // Organic navigation tag (~/aboutus/default.aspx = nt "aboutusdefault")
            Regex regex = new Regex("[~/\\!@#$%^&*()+=-]");
            requestPath = regex.Replace(requestPath, string.Empty).Replace(".aspx", string.Empty);
            string pageName = "~/page.aspx";
            string destinationUrl = VirtualPathUtility.ToAbsolute(pageName) + "?nt=" + requestPath;
            SendToNewUrl(destinationUrl, httpApp);
        }

        public void SendToNewUrl(string url, HttpApplication httpApp) {
            applyTrailingSlashHack(httpApp);
            httpApp.Context.RewritePath(
                url,
                false // RebaseClientPath must be false for ~/ to continue working in subdirectories.
            );
        }

        /// <summary>
        /// Applies the trailing slash hack. To circumvent an ASP.NET bug related to dynamically
        /// generated virtual directories ending in a trailing slash (/).
        /// As described by BuddyDvd:
        /// http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=105061
        /// </summary>
        /// <param name="httpApp">The HttpApplication.</param>
        /// <remarks>
        /// Execute this function before calling RewritePath.
        /// </remarks>
        private void applyTrailingSlashHack(HttpApplication httpApp) {
            if (httpApp.Request.Url.AbsoluteUri.EndsWith("/") && !httpApp.Request.Url.AbsolutePath.Equals("/")) {
                Type requestType = httpApp.Context.Request.GetType();
                object clientFilePath = requestType.InvokeMember("ClientFilePath", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty, null, httpApp.Context.Request, null);
                string virtualPathString = (string)clientFilePath.GetType().InvokeMember("_virtualPath", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetField, null, clientFilePath, null);
                clientFilePath.GetType().InvokeMember("_virtualPath", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField, null, clientFilePath, new object[] { virtualPathString });
                requestType.InvokeMember("_clientFilePath", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField, null, HttpContext.Current.Request, new object[] { clientFilePath });
                object clientBaseDir = requestType.InvokeMember("ClientBaseDir", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.GetProperty, null, httpApp.Context.Request, null);
                clientBaseDir.GetType().InvokeMember("_virtualPath", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField, null, clientBaseDir, new object[] { virtualPathString });
                requestType.InvokeMember("_clientBaseDir", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.SetField, null, HttpContext.Current.Request, new object[] { clientBaseDir });
            }
        }
    }
}

这篇关于自PageHandlerFactory为的.aspx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 06:26