简体   繁体   中英

Server.TransferRequest doesn't send passed header

My target is to send my own items of header of HTTP Response using Server.TransferRequest.

I'm trying to do something like that:

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();    

var headers = new NameValueCollection
{
    {"ErrorUrl", Context.Request.RawUrl},
    {"ErrorCode", code.ToString()}
};

Server.TransferRequest("~400.html", true, "GET", headers);

But Chrome shows me in Response Headers:

Accept-Ranges:bytes
Content-Length:573
Content-Type:text/html
Date:Wed, 10 Aug 2016 14:32:36 GMT
ETag:"a9d0dad4fbd1d11:0"
Last-Modified:Wed, 29 Jun 2016 11:46:10 GMT
X-Content-Type-Options:nosniff
X-Frame-Options:deny
X-SourceFiles:=?UTF-8?B?RDpcS0ZTXE1haW5cUHJvZHVjdFxLREMuVXNlcldlYlxLREMuVXNlcldlYi5XZWJSb2xlXHNyY1xWaWV3c1xlcnJvclxlbi1VU1w0MDMuaHRtbA==?=
X-XSS-Protection:1

Please, help me to get expected header. Thanks

Solved. I've created class inherit IHttpModule. I added event OnPreSendRequestHeaders. And I have access to headers I sent.

    public class HeadersModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.PreSendRequestHeaders += OnPreSendRequestHeaders;
        }

        private void OnPreSendRequestHeaders(object sender, EventArgs e)
        {
            HttpContext currentContext = HttpContext.Current;
            string errorUrl = currentContext.Request.Headers["ErrorUrl"];
        }

        public void Dispose()
        {
        }
    }

Also I added name to the web.config file. (See examples there https://msdn.microsoft.com/en-us/library/aa719858(v=vs.71).aspx )

This problem occurred because Server.TransferRequest performs an asynchronous execution of the specified URL using the specified HTTP method and headers. (See https://msdn.microsoft.com/en-us/library/aa344901(v=vs.110).aspx )

Now I can use errorUrl and modify response as I want

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM