Asp.net core 3 Web Api post request not working

user3661407 picture user3661407 · Dec 5, 2019 · Viewed 7.2k times · Source

I am trying to post a request to my controller just to post some data, but it is not working. It is not hitting the post method in the controller. I have tried so many things after searching on google, but it is still not working.

I am posting data by calling belwo url

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

and sending some data as form body.

Any help would be appreciated

Below is the controller code

[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);
        }

    }

}

and Here is my Startup.cs code

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();
        });
    }
}

Answer

Rena picture Rena · Dec 6, 2019

Besides,you need to change ILogger to 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()
    {
    }
    //..
}