MeTitus
MeTitus

Reputation: 3428

Controllers are not found in ASP.NET core in console application

I'm trying to get a a simple asp.net core projection set with just a basic configuration and a controller working but no avail so far. I did use the code from the web template project to create a new console project but no controllers are found.

Here is what the project looks like so far:

Project structure

Console output

Route not found

public class Program
{
    public static void Main(string[] args)
    {
        CreateWebHostBuilder(args).Build().Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseMvc();
    }
}

[ApiController]
public class TestController : ControllerBase
{
    [HttpGet]
    [Route("Test")]
    public ActionResult<string> Get()
    {
        return "Hello world!";
    }
}

//appsettings.json
{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

I'm missing something very obvious here but can't figure it out, any help is welcome.

Thanks

UPDATE 1

As per comment, trying http://localhost:5000/test/get

UPDATE 2

As per Damir's comment

Upvotes: 1

Views: 13353

Answers (2)

MeTitus
MeTitus

Reputation: 3428

I didn't pay attention but I had a warning in one of the dependencies and after adding them again, I got my original code working, thanks guys.

Added dependencies

Upvotes: 0

Damir Beylkhanov
Damir Beylkhanov

Reputation: 545

@marco

just add Route to your controller, it should be like

[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
    [HttpGet]
    [Route("Test")]
    public ActionResult<string> Get()
    {
        return "Hello world!";
    }
}

and url will look like: https://localhost:5001/api/test/test

UPDATE:

added screenshot

enter image description here

Upvotes: 3

Related Questions