【发布时间】: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();
}
}
【问题讨论】: