ASP.NET Core 3.0 Endpoint Routing doesn't work for default route

I have migrated an existing API project from 2.2 to 3.0 based on guidelines from this page.

Thus I've removed:

app.UseMvc(options =>
{
    options.MapRoute("Default", "{controller=Default}/{action=Index}/{id?}");
});

and inserted:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(name: "Default", pattern: "{controller=Default}/{action=Index}/{id?}");
});

But no controller and action would be bound. All I get for any API I call is 404.

How should I debug it and what do I miss here?

Update: The Startup.cs file is located in another assembly. We reuse a centralized Startup.cs file across many projects.

Upvotes: 9

Views: 15831

Answers (4)

Sammy360
Sammy360

Reputation: 171

It seems to not support conventional routing even in middleware like UseEndpoints and UseMvc You can find that here

Upvotes: 0

Ali Adravi
Ali Adravi

Reputation: 22733

You should try:

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });

Upvotes: 0

user8753842
user8753842

Reputation:

I can recommend my solution.

Create your CUSTOM base controller like that.

    [Route("api/[controller]/[action]/{id?}")]
    [ApiController]
    public class CustomBaseController : ControllerBase
    {
    }

And use CustomBaseController

 public class TestController : CustomBaseController
    {
        public IActionResult Test()
        {
            return Ok($"Test {DateTime.UtcNow}");
        }
    }

Rout` api/Test/test

Upvotes: 2

Xueli Chen
Xueli Chen

Reputation: 12695

From Attribute routing vs conventional routing:

It's typical to use conventional routes for controllers serving HTML pages for browsers, and attribute routing for controllers serving REST APIs.

From Build web APIs with ASP.NET Core: Attribute routing requirement:

The [ApiController] attribute makes attribute routing a requirement. For example:

[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase

Actions are inaccessible via conventional routes defined by UseEndpoints, UseMvc, or UseMvcWithDefaultRoute in Startup.Configure.

If you want to use conventional routes for web api , you need to disable attribute route on web api.

StartUp:

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

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "Default",
                pattern: "{controller=default}/{action=Index}/{id?}");
        });
    }

Web api controller:

 //[Route("api/[controller]")]
//[ApiController]
public class DefaultController : ControllerBase
{
    public  ActionResult<string> Index()
    {
        return "value";
    }

    //[HttpGet("{id}")]
    public ActionResult<int> GetById(int id)
    {
        return id;
    }
}

This could be requested by http://localhost:44888/default/getbyid/123

Upvotes: 11

Related Questions