【问题标题】:Custom HTTP handler for URL rewriting + session and application variable用于 URL 重写 + 会话和应用程序变量的自定义 HTTP 处理程序
【发布时间】:2012-10-05 11:11:22
【问题描述】:

目前我的产品页面网址是这样的

http://www.localhost:80/products/default.aspx?code=productCode

我想访问产品页面

http://www.localhost:80/productCode

我为此使用了 HTTP 模块。

public class UrlRewritingModule : IHttpModule

{

        public void Dispose()
        {

        }
        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);

           context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);


        }  
        void context_AuthorizeRequest(object sender, EventArgs e)
        {
             HttpContext context = ((HttpApplication)sender).Context;
            if (some condition)
            {
               context.RewritePath(url);
             }
        }
        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            //We set back the original url on browser
            HttpContext context = ((HttpApplication)sender).Context;

            if (context.Items["originalUrl"] != null)
            {
                context.RewritePath((string)context.Items["originalUrl"]);
            }
        }
}

我已经在 web.config 中注册了它,它工作正常。但是当我在 IIS 中部署它时,会话和应用程序变量不会抛出空引用异常。

谁能帮帮我?

编辑:是否需要额外的代码来访问会话/应用程序变量以重写 URL ?

【问题讨论】:

    标签: asp.net url-rewriting httpmodule vanity-url


    【解决方案1】:

    您是否尝试过使用 HTTPContext.Current?

    【讨论】:

    • 也许这会对你有所帮助stackoverflow.com/questions/276355/…
    • 感谢您的信息。我已经尝试过了,但是我的会话变量仍然抛出空引用异常。如果我尝试直接访问产品页面,那么一切正常。如果我们使用自定义处理程序,那么我们是否需要编写额外的代码来使用会话/应用程序变量?
    【解决方案2】:

    我能够通过在 web.config 的模块中添加 runAllManagedModulesForAllRequests="true" 属性来解决问题(访问由自定义处理程序重写的后续页面中的会话和应用程序变量)。

    【讨论】:

    • 这是一个非常糟糕的解决方案,基本上你绕过了静态文件处理,包括通过 ASP.Net 和所有其他模块和处理程序运行的缓存。你为什么不使用路线?
    猜你喜欢
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 2012-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多