Asp.net 核心 3 Web Api post 请求不工作

Asp.net core 3 Web Api post request not working

我正在尝试 post 向我的控制器发出请求,只是为了 post 一些数据,但它不起作用。它没有命中控制器中的 post 方法。在 google 上搜索后,我尝试了很多方法,但仍然无法正常工作。

我正在通过调用 belwo post 获取数据 url

POST: https://localhost:44341/api/FullPillarIdentifier/getIdentifierPutFileHandlingResponse

并将一些数据作为表单正文发送。

如有任何帮助,我们将不胜感激

控制器代码如下

[Route("api/[controller]")]
[ApiController]
public class FullPillarIdentifierController : BaseController
{

    private readonly IFullPillarRepository _pillarRepository;
    private readonly IXmlParser _xmlParser;
    private ILogger _logger;

    public FullPillarIdentifierController(ILogger logger, IFullPillarRepository pillarRepository, IXmlParser xmlParser)
    {
        _logger = logger;
        _xmlParser = xmlParser;
        _pillarRepository = pillarRepository;
    }



    // GET api/values
    [HttpPost]
    [Route("/getIdentifierPutFileHandlingResponse")]
    public IActionResult CreateMessageOnQueue([FromBody] string xml)
    {
        try
        {
            IdentifierPillarForPutFileRequest identifierPillarForPutFileRequest = _xmlParser.ToObject<IdentifierPillarForPutFileRequest>(xml);
            _pillarRepository.GetFileHandlingResponsePlan(identifierPillarForPutFileRequest);

            return Ok("Successfull");
        }
        catch (Exception e)
        {
            _logger.Log(new CoreLogging.Logging.LogMessage
            {
                ActionName = MemberMetaData.MethodName(),
                LoggingResponsibleSystem = "HermesWebApi",
                Exceptionn = e.Message
            }, Level.Error);

            return Error(e.Message);
        }

    }

}

这是我的Startup.cs代码

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(options =>
        {
            options.RespectBrowserAcceptHeader = true; // false by default

        }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
           .AddXmlSerializerFormatters()
           .AddXmlDataContractSerializerFormatters();

        services.AddCors();
    }

    public void ConfigureContainer(ContainerBuilder builder)
    {
        var module = new DependencyModule();
        module.Configuration = Configuration;
        builder.RegisterModule(module);

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }


        app.UseHttpsRedirection();
        app.UseCors();
        app.UseRouting();           
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

从动作的路线中删除前导斜杠 /,因为这会覆盖控制器上的路线。

//POST api/FullPillarIdentifier/getIdentifierPutFileHandlingResponse
[HttpPost]
[Route("getIdentifierPutFileHandlingResponse")]
public IActionResult CreateMessageOnQueue([FromBody] string xml)

Route templates applied to an action that begin with / or ~/ don't get combined with route templates applied to the controller.

引用Routing to controller actions in ASP.NET Core

此外,您需要将ILogger更改为ILogger<FullPillarIdentifierController>

[Route("api/[controller]")]
[ApiController]
public class FullPillarIdentifierController : BaseController
{
    private  ILogger<FullPillarIdentifierController> _logger;
    private readonly IFullPillarRepository _pillarRepository;
    private readonly IXmlParser _xmlParser;

    public FullPillarIdentifierController(ILogger<FullPillarIdentifierController> logger, IFullPillarRepository pillarRepository, IXmlParser xmlParser)
    {
        _logger = logger;
        _xmlParser = xmlParser;
        _pillarRepository = pillarRepository;
    }

    [HttpPost]
    [Route("getIdentifierPutFileHandlingResponse")]
    public IActionResult CreateMessageOnQueue([FromBody] string xml)
    {
        //...
    }

基地控制器:

[Route("api/[controller]/[action]")]
public class BaseController : Controller
{
    public BaseController()
    {
    }
    //..
}