0

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

kintela
  • 1,283
  • 1
  • 14
  • 32
  • I think the route is wrong, it should be `[Route("api/[controller]")]` – Max Jan 12 '23 at 16:16
  • If you are still struggling, could you please share which version of asp.net core you are using. – Md Farid Uddin Kiron Jan 12 '23 at 17:26
  • first, as Max said, check for the routing, you have a prefix there "API" second, will be the model binding mechanism that .net core uses, behind the scene you have your fields with uppercase letters and you need to check if the serializer that you are using takes that in consideration. Please share the Startup.cs as well to see what are you registering there – Zinov Jan 12 '23 at 17:53
  • I have updated with my Startup.cs code now. I'm using ASP.NET Core 3.1 – kintela Jan 13 '23 at 11:40

2 Answers2

1

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

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
0

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

kintela
  • 1,283
  • 1
  • 14
  • 32