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

4

9

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.

Roseliaroselin answered 21/10, 2019 at 14:42 Comment(2)
No they don't have any attribute. We only have one global default routing rule. Nowhere else we have created any other route, neither in action level, nor the controller level.Roseliaroselin
Probably way too late, but since there is no accepted answer: Do you have somewhere in the configuration the option options.EnableEndpointRouting = false? Because that tells the framework to continue using the 'old' routing, instead of endpoint routing.Iorio
B
11

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

Bionics answered 22/10, 2019 at 6:50 Comment(3)
We don't have any attribute other than HttpGet and HttpPost on our actions. And not attribute at all on our controllers.Roseliaroselin
@mohammadrostamisiahgelimo ,please show your Startup.cs .Bionics
I had to change back to UseMvc with old patterns.Roseliaroselin
I
2

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

Interpretive answered 20/11, 2019 at 17:39 Comment(0)
S
0

You should try:

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
Stepfather answered 16/11, 2021 at 2:15 Comment(0)
S
0

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

Saponin answered 14/5, 2022 at 18:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.