2

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?

asked Aug 7, 2020 at 16:26
1

2 Answers 2

3

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.

answered Jun 28, 2021 at 8:50

1 Comment

Route attribute definitions only become required if you are using the [ApiController] attribute.
1

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();
 });
answered Aug 7, 2020 at 16:53

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.