[英]Server.Transfer, Response.Redirect, and ApplicationInstance.CompleteRequest work around in ASP.Net
[英]How to set Response Header before Server.Transfer in Asp.Net?
我有一個頁面,根據某些條件我要么做Response.Redirect或Server.Transfer。 現在我想為兩個案例添加一個標題。 所以我正在做以下事情
Response.AddHeader("Vary", "User-Agent");
if (condition)
{
Server.Transfer(redirectUrl);
}
else
{
Response.Redirect(redirectUrl);
}
現在,當代碼通過Server.Transfer代碼路徑時,Vary標頭設置為*,而當它通過Response.Redirect時,標頭正確設置為User-Agent。
為什么會發生這種情況?如何為兩種情況設置響應標頭相同?
當您調用Server.Transfer
,當前頁面的Response對象將被目標頁面的Response對象(實際發送給用戶的Response) 替換 。 因此,如果要設置此特定標頭屬性,則必須在目標頁面上執行此操作。
如果它是有條件的,也許您可以使用HttpContext.Items
屬性,該屬性在第一頁上設置並在第二頁上讀取。
問候
Andre是正確的,Response對象被替換為Server.Transfer
一部分。 如果你想讓你轉移到父母不可知的頁面,你可以將信息打入HttpContext.Items
,然后使用IHttpModule
提取信息並適當地配置標題。 這樣的事情可能會起到作用......
Items.Add(VaryHttpModule.Key, "User-Agent");
if (condition)
{
Server.Transfer(redirectUrl);
}
else
{
Response.Redirect(redirectUrl);
}
public class VaryHttpModule : IHttpModule
{
public const string Key = "Vary";
public void Init(HttpApplication context)
{
context.PostRequestHandlerExecute +=
(sender, args) =>
{
HttpContext httpContext = ((HttpApplication)sender).Context;
IDictionary items = httpContext.Items;
if (!items.Contains(Key))
{
return;
}
object vary = items[Key];
if (vary == null)
{
return;
}
httpContext.Response.Headers.Add("Vary", vary.ToString());
};
}
public void Dispose()
{
}
}
干杯!
在Source.aspx
添加標頭,不要更改Destination.aspx page
的標頭。
如果要將結果頁面顯示為html,則應將標題content-type
添加為text/html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.