CORS和ASP.Net Web API [英] CORS and ASP.Net Web API

查看:88
本文介绍了CORS和ASP.Net Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在asp.net网络api中设置CORS。我的WebApiConfig.cs是:

I am attempting to get CORS set up in an asp.net web api. My WebApiConfig.cs is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Cors;
using System.Web.Http.Routing;

namespace WebApplication2
{
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();
        var cors = new EnableCorsAttribute("http://localhost", "*", "*");

        config.EnableCors(cors);
        config.Routes.MapHttpRoute(
            name: "bootstrap",
            routeTemplate: "abp/{controller}"
        );
    }
}
}

我还附加了标头我的控制器,即:

I also have appended headers in my Controller, which is:

namespace WebApplication2.Controllers
{
public class BootStrapController : ApiController
{
    public void Options(string locale, string deviceType)
    {
        string origin = HttpContext.Current.Request.Headers.Get("Origin") ??                                     "";
        HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Origin", origin);
        HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Methods", HttpContext.Current.Request.Headers["Access-Control-Request-Methods"]);
        HttpContext.Current.Response.AppendHeader("Access-Control-Allow-Headers", HttpContext.Current.Request.Headers["Access-Control-Request-Headers"]);
        HttpContext.Current.Response.End();
    }
    public object Get(string locale, string deviceType)
    {
        string origin = HttpContext.Current.Request.Headers.Get("Origin") ?? "";
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", origin);

但是,我在服务器响应中没有任何访问控制或任何附加的标头。如果您需要更多信息,请告诉我。

Yet, I do not have any access-control or any appended headers in the server response. If you need any more information let me know.

推荐答案

请访问 http://www.asp.net/web-api/overview/security/enabling-网络API中的跨域请求。您将获得在WebAPI中实现CORS的完整指南。

Please visit http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api. you will get a complete guide to implement CORS in WebAPI.

更新:
要在WEBAPI中实现CORS,请按照以下步骤操作:

UPDATE: To implement CORS in WEBAPI please follows these steps:


  1. 在解决方案中添加CORS NuGet软件包。在Visual Studio中,从工具菜单中选择库包管理器,然后选择 Package Manager Console 。在程序包管理器控制台窗口中,键入以下命令:

    Install-Package Microsoft.AspNet.WebApi.Cors

  1. Add the CORS NuGet package in your solution. In Visual Studio, from the Tools menu, select Library Package Manager, then select Package Manager Console. In the Package Manager Console window, type the following command:
    Install-Package Microsoft.AspNet.WebApi.Cors

打开文件 App_Start / WebApiConfig.cs 。将以下代码添加到 WebApiConfig.Register 方法。

Open the file App_Start/WebApiConfig.cs. Add the following code to the WebApiConfig.Register method.

public static class WebApiConfig
{
   public static void Register(HttpConfiguration config)
   {
    // New code
    config.EnableCors();    
  }
}


  • 接下来,添加[EnableCors]属性到BootStrapController类:

  • Next, add the [EnableCors] attribute to the BootStrapController class:

    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class BootStrapController  : ApiController
    {
        // Controller methods 
    }
    

    起源,标题和方法可能会根据您的需要而有所不同。

    origins,headers and methods may vary according to your need.

    这篇关于CORS和ASP.Net Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆