2015-05-28 35 views
0

我一直在試圖添加一個自定義頭的值到傳出響應消息。每個用戶請求的值都不相同。Restful WCF添加消息頭到傳出響應

但是我不能用下面的代碼添加具有價值的標題:

[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(); 
      } 
     } 

回答

0

它總是123,因爲你明確地將其與設置到:

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

下面是如何創建的終結點行爲,並正確地註冊它的一個示例的鏈接。 WCF blog from MS MVP

+0

現在它工作!非常感謝你 ! – Coach