【问题标题】:Restful WCF adding message header to outgoing responseRestful WCF 将消息头添加到传出响应
【发布时间】:2015-05-29 09:42:25
【问题描述】:

我一直在尝试为传出的响应消息添加一个具有价值的自定义标头。每个用户请求的值都不同。

但是我无法使用以下代码添加带有值的标头:

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

【问题讨论】:

    标签: c# wcf rest header


    【解决方案1】:

    它始终是 123,因为您明确地将其设置为:

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

    这里有一个链接,其中提供了如何创建端点行为并正确注册它的示例。 WCF blog from MS MVP

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-08
      相关资源
      最近更新 更多