2016-10-17 25 views
8

我想創建一個在MVC 5中與跨源請求(CORS)一起工作的Web應用程序。我嘗試了一切都沒有任何結果。如何在ASP.NET MVC中啓用交叉源請求

與屬性

public class AllowCrossSiteJsonAttribute: ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
      filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*"); 

     base.OnActionExecuting(filterContext); 
    } 
} 

隨着EnableCors屬性

[EnableCors("*")] 

沒有什麼工作,我開始認爲這是不可能

+1

你看了樣品:https://www.asp.net/web-api/overview/security/enabling-cross- origin-requests-in-web-api –

回答

3

要啓用跨域請求,該[EnableCors]屬性添加到您的Web API控制器或控制器的方法:

[EnableCors(origins: "http://systematixindia.com", headers: "*", methods: "*")] 


public class TestController : ApiController 
    { 
     // Controller method`enter code here`s not shown... 
    } 

Read More

+0

您的解決方案是針對Web Api,閱讀標題問題!它不適用於MVC。 –

0

啓用CORS在MVC 5(核心)首先需要將Microsoft.AspNetCore.Cors包添加到您的項目中。然後配置startup.cs這樣對所有網站

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddMvc(); 
    //Add Cors support to the service 
    services.AddCors(); 

    var policy = new Microsoft.AspNet.Cors.Core.CorsPolicy(); 

    policy.Headers.Add("*");  
    policy.Methods.Add("*");   
    policy.Origins.Add("*"); 
    policy.SupportsCredentials = true; 

    services.ConfigureCors(x=>x.AddPolicy("AllPolicy", policy)); 

} 


public void Configure(IApplicationBuilder app, IHostingEnvironment env) 
{ 
    // Configure the HTTP request pipeline. 

    app.UseStaticFiles(); 
    //Use the new policy globally 
    app.UseCors("AllPolicy"); 
    // Add MVC to the request pipeline. 
    app.UseMvc(); 
} 

,然後你也可以使用這樣

[EnableCors("AllPolicy")] 

詳細here

+0

錯誤:EnableCors不採用參數 –

+0

您正在使用哪個版本的asp.net mvc? – Mostafiz

+0

我正在使用MVC 5 –

8

添加的配置設置在你的web.config文件中設置的值爲Access-Control-Allow-OrigincustomHeaders這樣 -

<configuration> 
<system.webServer> 
    <httpProtocol> 
    <customHeaders> 
     <add name="Access-Control-Allow-Origin" value="*" /> 
    </customHeaders> 
    </httpProtocol> 
</system.webServer> 
</configuration> 

想要訪問thisthis以獲取更多詳細信息和其他選項。

0

我想你必須將它添加到「OnAuthentication」步驟或將配置添加到您的網絡配置。你可以試試我的代碼:)它的工作原理

public class AllowCrossSiteJsonAttribute : ActionFilterAttribute, IAuthorizationFilter 
    { 
     public void OnAuthorization(AuthorizationContext filterContext) 
     { 
      filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*"); 
     } 

    } 
14

我認爲最方便的是創建你自己的類,像這樣:

enter image description here

在它下面的代碼:

using System; 
using System.Web.Mvc; 

public class AllowCrossSiteAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost:4200"); 
     filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Headers", "*"); 
     filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Credentials", "true"); 

     base.OnActionExecuting(filterContext); 
    } 
} 

此之後,它的讓你使用這個裝飾上方法或整個控制器上

enter image description here enter image description here

你應該在此過程之後能夠看到您的響應標頭杜熱

enter image description here

感謝對這一response

0

我使用OWIN CORS實現(的NuGet Microsoft.Owin.Cors)啓用MVC控制器和Owin中間件一個Cors取得了成功,除了ApiControllers 。 Microsoft.AspNet.WebApi.Cors(使用config.EnableCors()[EnableCors]屬性)似乎只適用於ApiControllers。

有關示例代碼,請參閱http://benfoster.io/blog/aspnet-webapi-cors

0

您還可以使用下面的代碼允許跨域請求

public void ProcessRequest (HttpContext context) 
     { 
    context.Response.AddHeader("Access-Control-Allow-Origin" , "*"); 
} 
相關問題