【问题标题】:How can I access the request body in an IIS rewrite provider?如何访问 IIS 重写提供程序中的请求正文?
【发布时间】:2013-09-22 15:42:09
【问题描述】:

我使用IRewriteProvider interface 编写了一个自定义重写提供程序,并将其安装在 IIS 中。它正在工作,但我需要访问 request 的内容以及 URL。 newsgroup posting 表明我应该能够访问 HttpContext.Current,但在我的测试中它显示为 null

有没有办法从重写提供者那里获得对请求内容的访问权?

【问题讨论】:

  • Inside Rewrite() HttpContext.Current 为空??
  • @cheesemacfly:是的!任何关于为什么的想法都非常受欢迎!
  • 你想做什么?也许你可以在没有HttpContext.Current 的情况下解决它?
  • @cheesemacfly:我想根据请求的正文重写 URI。例如。如果请求正文为“1”,则在 URI 中添加“1”。
  • 我没有测试过,现在还不能,但你试过{ReplaceProvider:{ALL_HTTP}}{ReplaceProvider:{ALL_RAW}}吗?

标签: .net iis url-rewriting url-rewrite-module


【解决方案1】:

它很可能为 null,因为它从未处理任何上下文。如果你想根据内容修改 url,你应该在你的应用程序中实现一个自定义的 IHttpModule。

http://msdn.microsoft.com/en-us/library/ms972974.aspx

【讨论】:

  • 我认为我必须接受“你不能”作为答案。正如我在下面提到的,在我的例子中,HTTP 模块 don't work
  • 如果您在客户端代码中重新路由会怎样?如果您希望 myaddress.com/~myswitch 访问 myaddress.com/myApp/userPostData 那么为什么不在 javascript 中进行呢?
【解决方案2】:

一个用于重写 URL 的 IHttpModule 类...

public class UrlRewriteModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        try
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
            context.EndRequest += new EventHandler(context_EndRequest);
        }
        catch (Exception exc)
        {
            ...special logging of exc...
        }
    }
    void context_BeginRequest(object sender, EventArgs e)
    {
        string fullOrigionalpath = Request.Url.ToString();
        Context.RewritePath("...whatever you want...");
    }
}

还有 web.config...

<configuration>
  <system.web>
    <httpModules>
      <add name="UrlRewriteModule" type="UrlRewriteModule"/>
    </httpModules>

【讨论】:

猜你喜欢
  • 2020-08-31
  • 1970-01-01
  • 1970-01-01
  • 2011-01-15
  • 2015-03-20
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
  • 2015-03-27
相关资源
最近更新 更多