如何使用Angular 2在POST上启用ASP.NET MVC 4中的跨源请求

Fit*_*tch 7 javascript asp.net-mvc cors asp.net-mvc-4 angular

我实际上正在努力用CORS发出POST请求.

我实际上是一个在我的控制器内部的方法上添加正确响应头的类

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我就是这样用的

[AllowCrossSite]
public ActionResult GetReportParameters(string IdRapport)
Run Code Online (Sandbox Code Playgroud)

我得到了预期的结果 在此输入图像描述

但问题是当我尝试使用自定义标头发出POST请求以传递此特定内容类型时

'Content-Type': 'application/json; charset=utf-8'
Run Code Online (Sandbox Code Playgroud)

我实际上得到了这个响应头

在此输入图像描述

因此,即使我正确地进入属性类,也就像标题没有做任何事情一样.

这是我在Angular 2服务中的前端

const headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8' });
const options = new RequestOptions({ headers: headers , withCredentials: true });

const mock = {
  name: 'TitreRegroupement1',
  visible: false,
  type: 'System.String',
  value: 'Test'
};

// tslint:disable-next-line:max-line-length
const body = JSON.stringify({ idRapport: '00392024-d171-4f39-9c5c-97a51f53fd54', filtre: '', exportFormat: '', parameters: data });

return this.http.post('http://localhost:8080/ReportingViewer/ExportReport', body, options)
  .map((response: Response) => {
      return this.extractSingleData(response, Parameters);
  })
  .catch(this.handleError);
}
Run Code Online (Sandbox Code Playgroud)

通过使用postman声明没有问题,整个事情正确地旅行我可以从控制器内部的方法访问我的参数没有问题.

如果某个团体可以给我一些理解,我很高兴听到他们!:)

Fit*_*tch 5

我找到了使它工作的方法。首先,我应用提议给我的东西sideshowbarker

if (Request.Headers.AllKeys.Contains("Origin", StringComparer.OridinalIgnoreCase) &&
    Request.HttpMethod == "OPTIONS") {
    Response.Flush();
}
Run Code Online (Sandbox Code Playgroud)

并且我在标头中丢失了一些内容,我一直在执行“ *”,但最终使用了这些参数

filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
Run Code Online (Sandbox Code Playgroud)

对于那些对我课程的最终形式感兴趣的人

using System;
using System.Web;
using System.Web.Mvc;

namespace Via.Client.WebMvc
{
    public class AllowCrossSiteAttribute : System.Web.Mvc.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", "Origin, X-Requested-With, Content-Type, Accept");
            filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Credentials", "true");

            if (filterContext.HttpContext.Request.HttpMethod == "OPTIONS")
            {
                filterContext.HttpContext.Response.Flush();
            }

            base.OnActionExecuting(filterContext);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)