ASP.NET Core 3 Web Api发布请求不起作用

时间:2019-12-05 22:20:51

标签: c# asp.net-core-webapi asp.net-core-3.0 asp.net-core-routing

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

我正在通过调用belwo 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();
        });
    }
}

2 个答案:

答案 0 :(得分:2)

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

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

应用于以/~/开头的动作的路由模板不会与应用于控制器的路由模板组合在一起。

引用Routing to controller actions in ASP.NET Core

答案 1 :(得分:2)

此外,您需要将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)
    {
        //...
    }

BaseController:

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