kintela
kintela

Reputation: 1333

http post action not reached in ASP.NET Core 3.1 web API controller

I have this controller

[ApiController]
[Route("api/[controller]")]
public class PlanningController: ControllerBase
{
 public async Task<IActionResult> SaveTest([FromBody] TestData     data)
{      

  return Ok(data);

}

public class TestData
{
 public int Id { get; set; }
 public string Name { get; set; }
}

This in Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
      if (env.IsDevelopment())
      {
        app.UseDeveloperExceptionPage();
      }
      else
      {
       app.UseHsts();
      }

      app.UseCors("default");
      app.UseHttpsRedirection();
      app.UseRouting();
      app.UseAuthentication();
      app.UseAuthorization();

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

      app.Run(context =>    context.Response.WriteAsync("Planificador API iniciada"));
}

I put a break point in the return but when I post this in postman

enter image description here

enter image description here

nothing happens in my controller the break point is not reached.

I don't understand the response received in postman

In VS 2022 I see this

Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request    
starting HTTP/1.1 POST 
http://localhost:5001/api/planning/saveTest    application/json     34
Microsoft.AspNetCore.Hosting.Diagnostics[1] 
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request    finished in 8.3968ms 200 
Microsoft.AspNetCore.Hosting.Diagnostics[2]
Request finished in 8.3968ms 200 

Any idea, please?

Thanks

Upvotes: 0

Views: 258

Answers (2)

kintela
kintela

Reputation: 1333

Solved...

The problem was tha in my controller I was injected IFileProvider wrong

Unable to resolve service for type 'Microsoft.Extensions.FileProviders.IFileProvider' while attempting to activate my controller

Upvotes: 0

Md Farid Uddin Kiron
Md Farid Uddin Kiron

Reputation: 22495

Nothing happens in my controller the break point is not reached.I don't understand the response received in postman

Well, because of using [Route("planning")] before your PlanningController it is certainly overriding your application standard routing. So, your controller route has been changed. Thus, you shouldn't manipulate this routing [Route("api/[controller]")]

Correct Way:

    [Route("api/[controller]")]
    [ApiController]
    public class PlanningController : ControllerBase
    {
        [HttpPost]
        [Route("saveTest")]
        public async Task<IActionResult> SaveTest([FromBody] TestData data)
        {

            return Ok(data);

        }
    }

Update:

Stratup.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.AddControllers();
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "TestWebAPIProject", Version = "v1" });
        });
    }

    // 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.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TestWebAPIProject v1"));
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

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

Output:

enter image description here

Note: I would highly recommend you to have a look on our official document for Custom route constraints

Upvotes: 1

Related Questions