405关于在政策中使用AllowAnyMethod在Vue 3.0应用程序中使用axios到.Net Core 2.2的发布的响应

时间:2019-07-16 15:04:55

标签: vue.js axios asp.net-core-2.2

我已经迷失了方向,已经阅读了很多博客,SO问题和文档,我确信这是一个简单的修复程序,我现在完全不知道。

我有一个从vuejs应用程序到.net core 2.2 api项目的axios帖子调用。按照Microsoft的Enable CORS指南,我在服务中使用了访问策略并装饰了控制器。请参见下面的代码。

飞行前选项响应为204,实际调用时为405,引用allow:DELETE,GET,PUT作为允许的方法……我在这里错过了什么?我的策略中有.AllowAnyMethod,但它似乎被完全忽略了。使用WebAPI 2.2的同事。项目具有完全相同的代码,并且可以正常工作。

StartUp.cs

services.AddCors(options =>
{
  options.AddDefaultPolicy(
    builder =>
    {
      builder.WithOrigins("http://example.com",
                                    "http://www.contoso.com");
       });

   options.AddPolicy("VueFrontEnd",
       builder =>
       {
         builder.WithOrigins("http://localhost:30001/")
                                    .AllowAnyHeader()
                                    .AllowAnyMethod();
          });
      });

控制器

[EnableCors("VueFrontEnd")]
[HttpPost]
public async Task<JsonResult> DoesItExist(string searchString)
{            
  var data = new string[] { "A", "B", "C" };
  var result = data.Any(searchString.Contains);

  return Json(JsonConvert.SerializeObject(result));
 }

Vue

getClientsByPartialString(search: string) {
        Axios({
            method: 'post',
            url: 'https://localhost:44380/api/values/DoesItExist',
            crossDomain: true,
            data: {
                name: 'world',
            },
        })
    }

1 个答案:

答案 0 :(得分:0)

这让我很难过。正在路由。

添加[HttpPost(“ / TheApi”)]装饰器会对它进行排序。

我很ham愧。我使用了完整的URL http://localhost:port/api/values/themethod,尽管在控制器上进行了设置,但路由却使我失败。

与我有关的一件事是为什么它与GET和PUT一起使用并且仅在POST上失败。我没有答案。