Reputation: 3428
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:
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
UPDATE 2
Upvotes: 1
Views: 13353
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.
Upvotes: 0
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
Upvotes: 3