F#WebApi Cors不起作用
发布时间:2020-12-16 03:25:03 所属栏目:asp.Net 来源:网络整理
导读:我正在尝试创建一个用F#编写的WebApi项目并启用CORS. 控制者: type DoItController() = inherit ApiController() [EnableCors(origins = "*",headers = "*",methods = "Get")] [HttpGet] [Route "api/GetGenerateExcel"] member __.GetGenerateExcel() = as
我正在尝试创建一个用F#编写的WebApi项目并启用CORS.
控制者: type DoItController() = inherit ApiController() [<EnableCors(origins = "*",headers = "*",methods = "Get")>] [<HttpGet>] [<Route "api/GetGenerateExcel">] member __.GetGenerateExcel() = async { // Some code return "It works" } |> Async.StartAsTask } 直接从它工作的浏览器打开它.使用AngularJS或普通Javascript调用它我得到401. C#中的等效控制器工作原理: public class DoItController : ApiController { [EnableCors("*","*","Get")] [HttpGet] [Route("api/GetGenerateExcel")] public async Task<string> GetGenerateExcel() { //Some code return "It works" } } 我还检查了Global.asax,它们也匹配: type Global() = inherit HttpApplication() static member RegisterWebApi(config: HttpConfiguration) = config.EnableCors() config.MapHttpAttributeRoutes() config.Formatters.Remove(config.Formatters.XmlFormatter) |> ignore member x.Application_Start() = GlobalConfiguration.Configure(Action<_> Global.RegisterWebApi) public class Global : HttpApplication { protected void Application_Start() { GlobalConfiguration.Configure(config => { config.EnableCors(); config.MapHttpAttributeRoutes(); config.Formatters.Remove(config.Formatters.XmlFormatter); }); } } 我检查了所有的nuget包,他们匹配.此外,web.config没有可能影响功能的差异. 我还能检查或测试什么? 编辑: var request = new XMLHttpRequest(); request.open('GET',url,true); request.onload = function() { if (request.status >= 200 && request.status < 400) { // Success! } } request.onerror = function() { //Error } request.send(); AngularJS: $http.get(url).then(function(response){ // Success! },function(error){ // Error } 解决方法
所以我做了一点不同,但我相信同样的概念适用.我也在c#中,所以无视这些差异.
在我的WebApiConfig.cs中 using System.Web.Http.Cors; var cors = new EnableCorsAttribute("*","GET,POST,PUT,OPTIONS"); cors.SupportsCredentials = true; config.EnableCors(cors); config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi",routeTemplate: "api/{controller}/{id}",defaults: new { id = RouteParameter.Optional } ); 当我使用jQuery或JavaScript访问时,我需要使用我的jQuery请求发送以下内容.它授权用户访问. xhrFields : {withCredentials: true} 我没有像你那样使用Global.asax中的任何代码,因为我注册了指向App_Start文件夹中相关类的所有配置内容. 我的globabl.asax看起来像这样 protected void Application_Start() { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } 只是一个注释,我个人使用了一个xml文件的方法,我存储了可以访问webapi应用程序的授权域.代码有点不同,但我可以发布它,如果它会帮助任何人. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET通过自定义函数实现对字符串的大小写切换功能
- asp.net – 检测到SQL的非关闭连接
- 从ASP.NET MVC2向iPhone提供视频文件
- asp.net – 使用以前输入的值防止文本框自动填充
- ASP.NET Web API控制器专用串行器
- 使用jackson将asp.net / MS专有的json Dateformat转换为jav
- asp.net – .NET中的垃圾收集器
- 对VS 2019中ASP.NET Core项目解决:The term 'Add-Migr
- asp.net-mvc – [Authorize(Users =“*”)]在asp.net mvc中
- 配置 – ASP.NET Core Expose配置到DI注入服务
推荐文章
站长推荐
- azure – WebApi组织帐户 – 用户凭据验证失败
- 使用ASP.NET AJAX Control Toolkit设置焦点
- asp.net – Visual Studio 2005:是否有一个简单
- IdentityServer4 QuickStart 授权与自定义Claims
- ASP.NET Web窗体DropDownList具有SelectedValue,
- asp.net-mvc – ASP.NET MVC 5和HTML 5根据W3C规
- asp.net-mvc – ASP.NET MVC模型绑定外键关系
- 基于asp.net下使用jquery实现ajax的解决方法
- asp.net核心2.0发布到azure得到IIS 502.5错误
- asp.net-mvc如何更改宽度Html.TextBox
热点阅读