5

I am trying to create an API controller in my ASP.NET core 5 Razor Pages web application.

I have created the Api Controller inside a folder named Api in Visual studio.

Then when I try to run the following url to test that the api controller works, I get a 404 not found:

https://localhost:345345/api/saveimage

What am I missing?

Api Controller (SaveImageController.cs):

[Route("api/[controller]")]
[ApiController]
public class saveImageController : ControllerBase
{
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "test", "test2" };
    }
}
0

1 Answer 1

9

Even though using folder "Api" for your controller will work, it is normal to keep controllers inside a folder called "Controllers".

Please note that Razor pages are using folder based routing, the same does not apply to controllers.

In Startup#ConfigureServices, make sure you have this:

services.AddControllers();

In Startup#Configure, make sure you have this:

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
    endpoints.MapControllers();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! for some reason all the guides I checked failed to mention this

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.