2

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

2

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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;
    }
}

Comments

0

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;
        }
}

Comments

0
For Net Core 6.0 change this in your Program.cs
`var services = builder.Services;
services.AddControllers();
var app = builder.Build();
    if (app.Environment.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    app.UseHttpsRedirection();
    app.UseRouting();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
app.Run();`

and [Route("methodname")] use this attribute above your action method.

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.