如何在 asp.net 自托管 API 中启用 CORS?
How to enable CORS in asp.net Self-Hosted API?
我在 asp.net 中创建了一个自托管 Web API 当我从 POSTMAN 调用它时它工作正常但是当我从浏览器调用它时它给出以下错误。
对“http://localhost:3273/Values/GetString/1' from origin 'http://localhost:4200”处的 XMLHttpRequest 的访问已被 CORS 策略阻止:无“Access-Control-A
”
以下是我的服务class
using System.Web.Http;
using System.Web.Http.SelfHost;
namespace SFLSolidWorkAPI
{
public partial class SolidWorkService : ServiceBase
{
public SolidWorkService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
var config = new HttpSelfHostConfiguration("http://localhost:8069");
config.Routes.MapHttpRoute(
name: "API",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
HttpSelfHostServer server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
}
protected override void OnStop()
{
}
}
}
这里,
经过大量研究,我找到了解决这个问题的方法。
您只需要安装 Microsoft.AspNet.WebApi.Cors
并像 config.EnableCors(new EnableCorsAttribute("*", headers: "*", methods: "*"));
一样使用它
希望对其他人有所帮助。
谢谢
using System;
using System.ServiceProcess;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.SelfHost;
namespace SFLSolidWorkAPI
{
public partial class DemoService : ServiceBase
{
public DemoService ()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
var config = new HttpSelfHostConfiguration("http://localhost:8069");
config.Routes.MapHttpRoute(
name: "API",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.EnableCors(new EnableCorsAttribute("*", headers: "*", methods: "*"));
HttpSelfHostServer server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
}
protected override void OnStop()
{
}
}
}
我在 asp.net 中创建了一个自托管 Web API 当我从 POSTMAN 调用它时它工作正常但是当我从浏览器调用它时它给出以下错误。
对“http://localhost:3273/Values/GetString/1' from origin 'http://localhost:4200”处的 XMLHttpRequest 的访问已被 CORS 策略阻止:无“Access-Control-A
”以下是我的服务class
using System.Web.Http;
using System.Web.Http.SelfHost;
namespace SFLSolidWorkAPI
{
public partial class SolidWorkService : ServiceBase
{
public SolidWorkService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
var config = new HttpSelfHostConfiguration("http://localhost:8069");
config.Routes.MapHttpRoute(
name: "API",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
HttpSelfHostServer server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
}
protected override void OnStop()
{
}
}
}
这里,
经过大量研究,我找到了解决这个问题的方法。
您只需要安装 Microsoft.AspNet.WebApi.Cors
并像 config.EnableCors(new EnableCorsAttribute("*", headers: "*", methods: "*"));
希望对其他人有所帮助。
谢谢
using System;
using System.ServiceProcess;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.SelfHost;
namespace SFLSolidWorkAPI
{
public partial class DemoService : ServiceBase
{
public DemoService ()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
var config = new HttpSelfHostConfiguration("http://localhost:8069");
config.Routes.MapHttpRoute(
name: "API",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.EnableCors(new EnableCorsAttribute("*", headers: "*", methods: "*"));
HttpSelfHostServer server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
}
protected override void OnStop()
{
}
}
}