Adithyaa Veerapan
Adithyaa Veerapan

Reputation: 41

Routing in ASP.NET CORE Web API

How to use conventional and attribute routing in asp.net core web api?

Is it possible to combine both conventional and attribute routing similar to asp.net web api ?

How to specify default route in asp.net core web api?

Upvotes: 2

Views: 1316

Answers (2)

Darshani Jayasekara
Darshani Jayasekara

Reputation: 741

According to official Microsoft documentation, Attribute routing becomes a requirement in Asp.net core web API applications.

https://learn.microsoft.com/en-us/aspnet/core/web-api/index?view=aspnetcore-2.1#attribute-routing-requirement

You have to define attribute routes like below.

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase

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

Upvotes: 3

Rajdeep D
Rajdeep D

Reputation: 3920

In asp.net core web api and mvc you can specify routing

  1. Startup.cs in Configure method
  2. Controller

You can specify default routing in launchSettings.json. Set controller name at launchUrl property for all the profiles

.net core 2.2, At Startup.cs configure method,

app.UseMvc(routes =>
{
    routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});

.net core 3.1, At Startup.cs configure method,

app.UseRouting();

app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

Upvotes: 1

Related Questions