我在維護接受表單帖子並添加對CORS請求的支持的服務我在Firefox 3.6中遇到了這個問題,它使用OPTIONS請求標頭髮送預檢請求。支持aspx頁面中的OPTIONS請求標頭
我沒有添加必要的Access-Control-Allow-Origin響應頭與一個通用的http處理頁面有任何問題,但我有困難與完整的aspx頁面。這絕對不會觸及Page_Load,我無法弄清楚頁面生命週期中的哪個部分卡住了。
任何人有任何想法?
謝謝!
我在維護接受表單帖子並添加對CORS請求的支持的服務我在Firefox 3.6中遇到了這個問題,它使用OPTIONS請求標頭髮送預檢請求。支持aspx頁面中的OPTIONS請求標頭
我沒有添加必要的Access-Control-Allow-Origin響應頭與一個通用的http處理頁面有任何問題,但我有困難與完整的aspx頁面。這絕對不會觸及Page_Load,我無法弄清楚頁面生命週期中的哪個部分卡住了。
任何人有任何想法?
謝謝!
您可以用HttpModule
做到這一點,一個HttpHandler
我認爲一些這來自一個文章的地方,和它的其他部分被開發的房子......所以,如果它的一些來自其他地方,我提前道歉沒有給予應有的信用:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace YourNamespaceHere
{
using System;
using System.Web;
using System.Collections;
public class CrossOriginModule : IHttpModule {
public String ModuleName {
get { return "CrossOriginModule"; }
}
public void Init(HttpApplication application) {
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
}
private void Application_BeginRequest(Object source, EventArgs e) {
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
CrossOriginHandler.SetAllowCrossSiteRequestOrigin(context);
}
public void Dispose()
{
}
}
public class CrossOriginHandler : IHttpHandler
{
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
//Clear the response (just in case)
ClearResponse(context);
//Checking the method
switch (context.Request.HttpMethod.ToUpper())
{
//Cross-Origin preflight request
case "OPTIONS":
//Set allowed method and headers
SetAllowCrossSiteRequestHeaders(context);
//Set allowed origin
//This happens for us with our module:
SetAllowCrossSiteRequestOrigin(context);
//End
context.Response.End();
break;
default:
context.Response.Headers.Add("Allow", "OPTIONS");
context.Response.StatusCode = 405;
break;
}
context.ApplicationInstance.CompleteRequest();
}
#endregion
#region Methods
protected void ClearResponse(HttpContext context)
{
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.Clear();
}
protected void SetNoCacheHeaders(HttpContext context)
{
context.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
context.Response.Cache.SetValidUntilExpires(false);
context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetNoStore();
}
#endregion
public static void SetAllowCrossSiteRequestHeaders(HttpContext context)
{
string requestMethod = context.Request.Headers["Access-Control-Request-Method"];
context.Response.AppendHeader("Access-Control-Allow-Methods", "GET,POST");
//We allow any custom headers
string requestHeaders = context.Request.Headers["Access-Control-Request-Headers"];
if (!String.IsNullOrEmpty(requestHeaders))
context.Response.AppendHeader("Access-Control-Allow-Headers", requestHeaders);
}
public static void SetAllowCrossSiteRequestOrigin(HttpContext context)
{
string origin = context.Request.Headers["Origin"];
if (!String.IsNullOrEmpty(origin))
context.Response.AppendHeader("Access-Control-Allow-Origin", origin);
else
//This is necessary for Chrome/Safari actual request
context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
}
}
}
而且在Web.config:
...
<system.webServer>
...
<modules runAllManagedModulesForAllRequests="true">
...
<add name="CrossOriginModule" preCondition="managedHandler" type="YOURNANMESPACEHERE.CrossOriginModule, ASSEMBLYNAME" />
</modules>
<handlers>
<add name="CrossOrigin" verb="OPTIONS" path="*" type="YOURNAMESPACEHERE.CrossOriginHandler, ASSEMBLYNAME" />
</handlers>
</system.webServer>
史蒂夫的回答我令人驚訝的是,它不可避免地導致我在案件中得到解決,這就是爲什麼我投了票。但是,在我看來,HttpHandler可能並不是明確需要的。所以我將CORS嚴格配置在插入請求管道的模塊中。我的代碼:
using System;
using System.Web;
namespace NAMESPACE.HttpModules
{
public class CrossOriginModule : IHttpModule
{
public String ModuleName
{
get { return "CrossOriginModule"; }
}
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
}
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
string httpMethod = context.Request.HttpMethod.ToUpper();
//preflight
if (httpMethod == "OPTIONS")
{
ClearResponse(context);
//Set allowed method and headers
SetAllowCrossSiteRequestHeaders(context);
//Set allowed origin
SetAllowCrossSiteRequestOrigin(context);
//end request
context.ApplicationInstance.CompleteRequest();
}
else
{
SetAllowCrossSiteRequestOrigin(context);
}
}
static void SetAllowCrossSiteRequestHeaders(HttpContext context)
{
string requestMethod = context.Request.Headers["Access-Control-Request-Method"];
context.Response.AppendHeader("Access-Control-Allow-Methods", "GET,POST");
//We allow any custom headers
string requestHeaders = context.Request.Headers["Access-Control-Request-Headers"];
if (!String.IsNullOrEmpty(requestHeaders))
context.Response.AppendHeader("Access-Control-Allow-Headers", requestHeaders);
//allow credentials
context.Response.AppendHeader("Access-Control-Allow-Credentials", "true");
}
static void SetAllowCrossSiteRequestOrigin(HttpContext context)
{
string origin = context.Request.Headers["Origin"];
if (!String.IsNullOrEmpty(origin))
context.Response.AppendHeader("Access-Control-Allow-Origin", origin);
else
context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
}
static void ClearResponse(HttpContext context)
{
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.Clear();
}
public void Dispose()
{
}
}
}
而在你web.config
<modules runAllManagedModulesForAllRequests="true">
<add name="CrossOriginModule" preCondition="managedHandler" type="NAMESPACE.HttpModules.CrossOriginModule" />
</modules>
這將配置響應報頭根據需要,讓MVC處理請求,因爲它通常會。
不確定你的意思,因爲Web服務沒有頁面加載事件。通常,Web服務只是具有webmethod屬性的單個函數。 http://msdn.microsoft.com/en-us/library/system.web.services.webservice.aspx – Hogan
對不起,「服務」的混淆使用。這是一個網頁,其中有一個接受POST的頁面。我明白,它不需要是一個aspx頁面,但是已經鏈接到它的野外頁面因此無法輕易更改。 – ari