【问题标题】:how to enable CORS in ASP.NET MVC Controller Level in WEB API? [duplicate]如何在 WEB API 的 ASP.NET MVC 控制器级别中启用 CORS? [复制]
【发布时间】:2019-08-10 02:49:10
【问题描述】:

我正在开发 WebApi,并且为 WebAPI.config 级别启用了 CORS,但未为 MVC 控制器启用它? 我不是在问 .Net CORE!

【问题讨论】:

  • 我不这么认为@Mikev,看看标签,他没有专门要求.NET Core

标签: c# asp.net .net api


【解决方案1】:

查看https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api

上面写着:

using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;

namespace WebService.Controllers
{
    [EnableCors(origins: "http://mywebclient.azurewebsites.net", headers: "*", methods: "*")]
    public class TestController : ApiController
    {
        // Controller methods not shown...
    }
}

所以使用 EnableCors 属性。祝你好运。

【讨论】:

    猜你喜欢
    • 2018-02-01
    • 2018-06-18
    • 2021-08-13
    • 1970-01-01
    • 2017-05-03
    • 2016-05-20
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多