aspnet / Routing

[Archived] Middleware for routing requests to application logic. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
272 stars 122 forks source link

Getting 404 with attribute routing in ASPNetCore 2.1.4 #894

Closed vkukke closed 5 years ago

vkukke commented 5 years ago

I'm trying to setup Web APIs using aspnetcore 2.1.4.

I have a console app which does the following:- IWebHost managementHost = ManagementHostBuilder.Build(); managementHost.Start();

I have another dll where I have

  1. WebHostBuilder
  2. Startup
  3. Controller

Code snippets are provided below WebHostBuilder Has mostly configuring kestrel and sets up the startup class (ManagementStartup)

Startup Class

public IServiceProvider ConfigureServices(IServiceCollection services)
{
   services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
   // other stuff
}

public void Configure(IApplicationBuilder app)
{
   // other stuff
  app.UseMvc();
}

Controller class

[Produces("application/json")]
[Route("api/[controller]")]
[ApiController]
public class ProductsController : ControllerBase
{
      [HttpPost]
       public Task<ActionResult> CreateProductAsync(ProductInfo productInfo)
        {
            return Task.FromResult<ActionResult>(new OkResult());
        }
        [HttpGet("{id}")]
        public Task<ActionResult> GetProductAsync(int id)
        {
            return Task.FromResult<ActionResult>(new OkResult());
        }

        [HttpGet]
        public Task<ActionResult> GetProducts()
        {
            return Task.FromResult<ActionResult>(new OkResult());
        }
 } 

After I build and hit F5 I do a curl as follows:- curl --header "Content-Type: application/json; charset=utf-8" http://127.0.0.1:5889/api/products

My controller is not being hit. What am I missing. The port number is what I have configured kestrel to run under.

mkArtakMSFT commented 5 years ago

Hi @vkukke. It looks like your APIs require and id parameter. Try to remove the id parameter from the GetProducts API.

vkukke commented 5 years ago

yikes. its a typo. Apologies. Removed it and same result. FYI

    [HttpGet]
    public Task<ActionResult> GetProducts()
    {
        return Task.FromResult<ActionResult>(new OkResult());
    }
mkArtakMSFT commented 5 years ago

Closing this issue as it's resolved now.