簡體   English   中英

寧靜的WCF將消息頭添加到傳出響應

[英]Restful WCF adding message header to outgoing response

我一直在嘗試將具有值的自定義標頭添加到傳出響應消息中。 每個用戶請求的值都將不同。

但是,我無法使用以下代碼為標頭添加值:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service : IMyService
{
    public string CommandHandler()
    {
        string s = "test";
        WebOperationContext.Current.OutgoingResponse.Headers.Add("testheader", "123456");
        return s;
    }
}

如果將以下代碼添加到global.asax ,則可以,但是testheader始終為123 ,我無法更改該值。

protected void Application_BeginRequest(object sender, EventArgs e) 
        {

            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET");
                HttpContext.Current.Response.AddHeader("testheader", "123");

                HttpContext.Current.Response.End();
            }
        }

始終為123,因為您通過以下方式將其顯式設置為123:

HttpContext.Current.Response.AddHeader("testheader", "123");

以下是有關如何創建端點行為並正確注冊它的示例的鏈接。 MS MVP的WCF博客

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM