locked
asp.net core webapi is not hitting or the frombody parameter is coming as null RRS feed

  • Question

  • User1838940990 posted

    Hi All,

    I am very new to  the webapi and am developing a api from the scratch (asp.net core 3.0)

    IT has a functionality to update and insert the xml.

    Please excuse , i have used the verbs for the api methods to easily explain the problem

    PFB my controller and the startup.cs file 

    scenario1

    If I select the content as JSON and if I pass a string in quotes  for eg ("This is JSON string "), both the put and post methods are getting hit and   I can see the string value is passed in the parameter and I can capture the value from the FromBody parameter,

    but if I pass the XML  and tried setting the options as JSON and tried to send , but didnt worked

    scenario2: 

    If I pass the value as XML  or Text from the postman and  select the option as zml or text and If I paste a sample XML in Raw>>Body  it is not even hitting the API method,  the request just reached the controllers constructor and return 

    I even tried changing [FromBody] string jobXml   to XMLDocument type as well  [FromBody] XmlDocument jobXml 

    can anybody help me to find If am doing anything wrong here.

    Controller class

    [Route("myapi/[controller]")]
    public class JobController : Controller
    {

    [HttpPost]
    [Route("updatejob")]
    public async Task<string> UpdateJobAsync( [FromBody] string jobXml)
    {
    doSoemthing;
    }


    [HttpPut]
    [Route("putjob")]
    public async Task<string> PutJobAsync([FromBody] string jobXml)
    {
    doSoemthing;
    }

    startup class

    public class Startup
    {

    public IConfiguration Configuration { get; }

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

    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
    services.AddCors();
    services.AddControllers();
    services.Configure<AXConfig>(Configuration.GetSection("AXConfig"));
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
    if (env.IsDevelopment())
    {
    app.UseDeveloperExceptionPage();
    }

    app.UseRouting();

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

    Monday, February 10, 2020 6:05 AM

Answers

  • User711641945 posted

    Hi techasuran,

    First,you need to add support for xml:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers().AddXmlSerializerFormatters();
    }

    Then you could send request like below:

    Controller:

    [HttpPost]
    [Route("updatejob")]
    public async Task<string> UpdateJobAsync([FromBody] XmlDocument jobXml)
    {
        //do your stuff...

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 10, 2020 8:26 AM

All replies

  • User711641945 posted

    Hi techasuran,

    First,you need to add support for xml:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers().AddXmlSerializerFormatters();
    }

    Then you could send request like below:

    Controller:

    [HttpPost]
    [Route("updatejob")]
    public async Task<string> UpdateJobAsync([FromBody] XmlDocument jobXml)
    {
        //do your stuff...

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 10, 2020 8:26 AM
  • User1838940990 posted

    Thanks very much for  the quick reply. Its working fine now.

    Wednesday, February 12, 2020 5:25 AM