Home > Software engineering >  ASP.NetCore API controller does not call my method in my Controller
ASP.NetCore API controller does not call my method in my Controller

Time:02-05

I create an API Controller in my ASP.NET core web api project.

    [ApiController]
    [Route("[controller]")]
    public class TestController : ControllerBase
    {

        public MyData Method1(string Id)
        {
            Console.WriteLine("here");
            return null;
        }

        public MyData Method2()
        {
            Console.WriteLine("here");
            return null;
        }
}

I set breakpoints in each method and load these urls in my browser: https://localhost:44357/test/Method1/1343a https://localhost:44357/test/Method2

And in my Startup.cs, I have

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
});

Can you please help me why my API controller does not get invoked when I load ^ urls in browser? I read https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-6.0 , I think those method should get called.

Thank you for your help.

CodePudding user response:

Please change your route to include the action like below

[ApiController]
[Route("[controller]/[action]")]
public class TestController : ControllerBase
{

    public MyData Method1(string Id)
    {
        Console.WriteLine("here");
        return null;
    }

    public MyData Method2()
    {
        Console.WriteLine("here");
        return null;
    }
}

CodePudding user response:

First check by adding verb over method [HTTPPOST] or [HTTPGET].

Also your starup.cs code looks like as written below:

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


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

    app.UseHttpsRedirection();
    app.UseRouting();

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

You controller Code:

[ApiController]
    [Route("api/[controller]")]
    public class TestController : ControllerBase
    {
        [HttpGet]
        [Route("method1")] 
        public MyData Method1(string Id)
        {
            Console.WriteLine("here");
            return null;
        }

        
        [HttpGet]
        [Route("method2")] 
        public MyData Method2()
        {
            Console.WriteLine("here");
            return null;
        }
}

Now call you API like :

https://localhost:portno/api/test/method1/abc

https://localhost:portno/api/test/method2

CodePudding user response:

in Asp.net Api you need route for each Action:

 [ApiController]
    [Route("[controller]")]
    public class TestController : ControllerBase
    {
        [HttpGet("[Action]/{Id}")]
        public MyData Method1(string Id)
        {
            Console.WriteLine("here");
            return null;
        }
        [HttpGet("[Action]")]
        public MyData Method2()
        {
            Console.WriteLine("here");
            return null;
        }
}

CodePudding user response:

In the API controller the HttpGet is used by default:

[ApiController]
[Route("[controller]/[action]")]
public class TestController : ControllerBase
{
    [Route("{id}")]
    public MyData Method1(string Id)
    {
        Console.WriteLine("here");
        return null;
    }

    public MyData Method2()
    {
        Console.WriteLine("here");
        return null;
    }
}

In the startup.cs:

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

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

Now the API method can be called by:

  • https://localhost:44357/test/Method1/1343a

  • https://localhost:44357/test/Method2

  •  Tags:  
  • Related