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?

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.

Sign up to request clarification or add additional context in comments.

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();
            });

Comments

Your Answer

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.