How can I access the request body in an IIS rewrit

2019-03-02 15:03发布

I've written a custom rewrite provider using the IRewriteProvider interface, and installed it in IIS. It's working, but I need to access the content of the request as well as the URL. A newsgroup posting suggests that I should be able to access HttpContext.Current, but in my testing it shows up as null.

Is there any way to get access to the request content from a rewrite provider?

2条回答
干净又极端
2楼-- · 2019-03-02 15:55

An IHttpModule class for rewriting the URL...

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

And the web.config...

<configuration>
  <system.web>
    <httpModules>
      <add name="UrlRewriteModule" type="UrlRewriteModule"/>
    </httpModules>
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-03-02 15:58

It's most likely null because it never processed any context. If you'd like to modify the url based on content, you should implement a custom IHttpModule in your application.

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

查看更多
登录 后发表回答