How to add Web API controller to an existing ASP.NET Core MVC?

Viewed 20226

I created a project using the default ASP.NET Core MVC template. I would like to also create a RESTful API under /api/{Controller}. I added a new Web API controller (standard Web API controller class template) but I can't call it. I get an error saying that the page cannot be found. I tried adding a route in Startup.cs but it doesn't do anything:

app.UseMvc(routes =>
{
    routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");
    routes.MapRoute(name: "api", template: "api/{controller=Admin}");
});

EDIT:

Like I said, it's all default templates. Here's the Web API Controller that I added:

[Route("api/[controller]")]
public class AdminController : Controller
{                
    // GET api/values/5
    [HttpGet("{id}")]
    public string Get(int id)
    {
        return "value";
    }

    // POST api/values
    [HttpPost]
    public void Post([FromBody]string value)
    {
    }

    // PUT api/values/5
    [HttpPut("{id}")]
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/values/5
    [HttpDelete("{id}")]
    public void Delete(int id)
    {
    }
}
6 Answers

I had luck doing this with v3.1:

Add Folder Controllers to the project. Add Controller, named TestController, to the folder. Then add the following to the Startup.cs:

services.AddControllers();

to

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

and:

endpoints.MapControllers();

to

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

Then I was able to call /api/Test.

If someone still have problem in adding webapi to .net core MVC, just inserting [ApiController] and [Route("api/[controller]")] attributes before class solves the problem:

[Route("api/[controller]")]
[ApiController]
public class ListController
{ ... }

I didn't added a route mapping in Startup.cs and still working well. The version of .net that I am using is 2.1.402

After updating to the latest version of ASP.NET Core, v2.0.1 (the one that needs VS2017), the problem resolved itself. I think it was probably a bug or shortcoming in the old version.

Running existing .net core MVC:

1

In order to add api controllers to existing .net core 6 mvc application, Open Program.cs file, add following codes:

//for apis
builder.Services.AddRazorPages();
builder.Services.AddControllers();

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

Now go ahead and add a new controller:

[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
    //endpoint: http://localhost:5207/api/test/strings
    [HttpGet("strings")]
    public IActionResult TestMethod()
    {
        return Ok(new string[]{ 
        "23", "24"});
    }
}

Testing Endpoint here

2

This works for me in .net core 6

Related