简体   繁体   English

开发环境中的 CORS 错误; 本地工作正常

[英]CORS error in dev environment; works OK locally

I am trying to make an api call from a vue application to a .net core web api.我正在尝试从 vue 应用程序向 .net core web api 进行 api 调用。 Locally this works, when we then run it on our first dev environment we get this when it trys to make the request在本地这是有效的,当我们然后在我们的第一个开发环境中运行它时,我们会在它尝试发出请求时得到这个

Access to XMLHttpRequest at ' https://bla-api/api/foc?page=1&pageSize=10&sortBy=&sortDirection=ASC ' from origin ' https://bla-api ' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.在“ https://bla-api/api/foc?page=1&pageSize=10&sortBy=&sortDirection=ASC ”从源“ https://bla-api ”访问 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应没有't pass access control check: 请求的资源上不存在'Access-Control-Allow-Origin'标头。

I have added the following to the C#' Startup file in the ConfigureServices method.我已将以下内容添加到ConfigureServices方法中的 C# 启动文件中。 To just get it working I wanted to provide the *(wildcard), so let anyone call this.为了让它工作,我想提供*(通配符),所以让任何人都称之为。 So presume that the named policy AllowAnyOrigin would do this!?所以假设命名策略 AllowAnyOrigin 会这样做!?

readonly string _SpecificOrigins = "AllowSpecificOrigins";

services.AddCors(options =>
{
    options.AddPolicy(_SpecificOrigins,
        builder =>
        {
            builder.AllowAnyOrigin()
                .AllowAnyHeader()
                .AllowAnyMethod();
        });
})

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

then also added this in the Configure method然后也在配置方法中添加了这个

app.UseCors(_SpecificOrigins);
app.UseMvc();

What am I missing?我错过了什么? I also try and make a fiddler request to the api and it breaks.我还尝试向 api 发出提琴手请求,但它中断了。 Any help would be greatly appreciated任何帮助将不胜感激

This was a red herring, the web api was not even deployed correctly, it was being deployued as a kestrel exe but was deployed as an azure web app.这是一个红鲱鱼,甚至没有正确部署 web api,它被部署为 kestrel exe,但被部署为azure web 应用程序。 Adding a web.config pointing to the main api project resolved the issue.添加指向主 api 项目的 web.config 解决了该问题。 I did not write the deployment so that no needs looking at next.我没有写部署,所以不需要看下一步。 Presume the above question is fine then, cheers for the help那么假设上述问题没问题,为帮助欢呼

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 表单身份验证在本地工作,但不在开发服务器上 - Forms Authentication works locally, but not on dev server includeExceptionDetailInFaults = true在开发环境中有效,但在测试环境中无效 - includeExceptionDetailInFaults=true works in dev-environment, but not in test-environment C#中的Windows Service在开发机上运行,​​在测试环境中崩溃 - Windows Service in C# works on dev machine, crashes in test environment NLog似乎没有登录ClickOnce环境,但在dev中工作 - NLog doesn't seem to be logging in ClickOnce environment but works in dev Azure网站程序集相关性错误-本地工作 - Azure site assembly dependency error - Works locally 使用CORS通过IIS访问WCF REST服务在本地工作,但不能从连接的设备工作 - Accessing WCF REST service through IIS using CORS works locally but not from connected device 更新面板中的按钮不能在INT环境中工作,但在Dev环境中工作正常(两者都在同一服务器中) - Button inside update panel is not working on INT Environment but works fine in Dev environment (both are in the same server) PDF在本地渲染但不在dev上渲染 - Pdf render locally but not on dev 我的 Azure 开发环境总是将我重定向到错误页面 - My Azure dev environment always redirects me to an error page 在开发环境上获取CS1502编译器错误,但未在生产环境中获取 - getting CS1502 compiler error on dev environment but not production
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM