Javascript 反应JS+;Azure MVC:进行API调用时发布404(未找到)

Javascript 反应JS+;Azure MVC:进行API调用时发布404(未找到),javascript,c#,reactjs,redux,Javascript,C#,Reactjs,Redux,我正在从我的React应用程序向Azure MVC控制器进行POST API调用。但是,当我这样做时,我在控制台中收到以下错误: 职位http://localhost:3000/api/SampleData/AcknowledgeRole 404(不是 发现) 考虑到我已经在Azure MVC控制器中指定了路由,这似乎很奇怪。下面是我的控制器: namespace Microsoft.IT.Security.OneAuthZUI.Controllers { using System;

我正在从我的React应用程序向Azure MVC控制器进行POST API调用。但是,当我这样做时,我在控制台中收到以下错误:

职位http://localhost:3000/api/SampleData/AcknowledgeRole 404(不是 发现)

考虑到我已经在Azure MVC控制器中指定了
路由,这似乎很奇怪。下面是我的控制器:

namespace Microsoft.IT.Security.OneAuthZUI.Controllers
{
    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.IdentityModel.Tokens.Jwt;
    using System.IO;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Cors;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Azure.Cosmos;
    using Microsoft.Azure.KeyVault;
    using Microsoft.Azure.Services.AppAuthentication;
    using Microsoft.Enterprise.Authorization.Client.Legacy;
    using Microsoft.Extensions.Configuration;
    using Microsoft.IT.Security.OneAuthZUI.WebServices;
    using Microsoft.IT.Security.OneAuthZUI.WebServices.Helpers;
    using Microsoft.Net.Http.Headers;

    ////[Authorize]
    [Route("api/[controller]")]
    [ApiController]
    [EnableCors("AllowSpecificOrigin")]
    public class SampleDataController : Controller
    {
        [HttpPost("[action]")]
        [Authorize]
        public void AcknowledgeRole(string acknowledgementInfo)
        {
            string[] components = acknowledgementInfo.Split("&");
            string assignmentId = components[0];
            string changeId = components[1];
            Console.WriteLine("assignmentId = " + assignmentId);
            Console.WriteLine("changeId = " + changeId);
        }
    }
}
下面是我进行POST API调用的函数:

  public roleAcknowledge(roleAssignmentId: string, changeId: string) {
    return function() {
      // Send a POST request to the controller, passing in the roleAssignmentId and the changeId
      var xhr = new XMLHttpRequest();
      xhr.open('POST', 'api/SampleData/AcknowledgeRole', true);
      xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

      // Use concatenation to send multiple parameters
      var parameters = roleAssignmentId + "&" + changeId;

      xhr.send(parameters);
    }
  }

试着像这样改变函数

   [HttpPost]
   [ActionName("AcknowledgeRole")]
   [Authorize]

我刚刚尝试使用了
[HttpPost(“[action]”)
,但它确实起了作用,这是我以前没有意识到的,因为我通常使用
[Route]
属性。我猜您面临的问题与您的
cors
设置有关。不确定如何配置
cors
,但由于您只是在本地主机上运行应用程序,请尝试将其配置为允许任何,然后查看它是否可以工作。请确保不要将其部署到生产环境中

services.AddCors(o => o.AddPolicy("AllowAnyPolicy", builder => builder
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()));

或者,只需暂时删除
[EnableCors(“allowSpecificCorigin”)]

url显示本地主机,但您在azure@Sajeetharan抱歉,我正在本地运行代码,而不是在Azure上运行:您的控制器似乎正常,您需要将您的方法参数修饰为
void ackknowledgeRole([FromQuery]string acknowledgementInfo)
。不要在查询字符串中使用“&”作为限定符来限定2个字符串,而是使用不同的限定符,因为“&”是一个特殊的字符分割查询字符串,“&”之后的字符串将被视为下一个查询字符串项。不幸的是,我仍然得到以下错误:404(未找到)另外,您不必在控制器中再次定义路由。您可以尝试调试吗?