Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 557
mohammad rostami siahgeli Avatar asked Oct 21 '19 14:10

mohammad rostami siahgeli


People also ask

How would you setup the default route using endpoints?

UseEndpoints(endpoints => { endpoints. MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); Inside the call to UseEndpoints() , we use the MapControllerRoute() method to create a route by giving the name default .

How do I enable routing in .NET Core?

Inside the ConfigureRoute method, you can configure your routes; you can see that this method has to take a parameter of type IRouteBuilder. The goal of routing is to describe the rules that ASP.NET Core MVC will use to process an HTTP request and find a controller that can respond to that request.

How does core routing work in asp net?

Routing uses a pair of middleware, registered by UseRouting and UseEndpoints: UseRouting adds route matching to the middleware pipeline. This middleware looks at the set of endpoints defined in the app, and selects the best match based on the request. UseEndpoints adds endpoint execution to the middleware pipeline.


1 Answers

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

like image 104
Xueli Chen Avatar answered Sep 28 '22 05:09

Xueli Chen