How can I access the request body in an IIS rewrite provider?

旧街凉风 提交于 2019-12-20 03:52:42

问题


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?


回答1:


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




回答2:


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>


来源:https://stackoverflow.com/questions/17733444/how-can-i-access-the-request-body-in-an-iis-rewrite-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!