2

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();
        });
    }
}
1
  • What error did you make?I could get 404 at first.After i change the correct url I would get the error:Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activate 'FullPillarIdentifierController'.And what is your BaseController? Commented Dec 6, 2019 at 2:36

2 Answers 2

3

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()
    {
    }
    //..
}
Sign up to request clarification or add additional context in comments.

Comments

2

Remove the leading slash / from the route on the action as this is overriding the rout on the controller.

//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.

Reference Routing to controller actions in ASP.NET Core

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.