【发布时间】:2014-06-22 23:53:52
【问题描述】:
在我的 WCF 服务中,我收到了 405 method not allowederror,然后遇到了一个帖子,建议在我的 WCF 主机的 Application_BeginRequest 中包含以下内容:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
"Accept, Content-Type,customHeader");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods",
"POST,GET,OPTIONS");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age",
"172800");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials",
"true");
HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers",
"customHeader");
HttpContext.Current.Response.AddHeader("Content-type",
"application/json");
HttpContext.Current.Response.End();
}
else
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
"Accept, Content-Type,customHeader");
HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers",
"customHeader");
HttpContext.Current.Response.AddHeader("Content-type",
"application/json");
}
}
但我正在使用控制台应用程序托管我的服务。
using (ServiceHost sc = new ServiceHost(typeof(DataRetriever)))
{
sc.Open();
foreach (var endPoints in sc.Description.Endpoints)
{
Console.WriteLine(endPoints.Address);
}
Console.ReadKey();
sc.Close();
}
那么如何在控制台应用程序中包含标题。
【问题讨论】:
-
我的情况一模一样,你找到解决办法了吗?
标签: c# wcf cross-domain same-origin-policy