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?
-
1Does this answer your question? asp.net core web api center routingSh.Imran– Sh.Imran2020年08月07日 17:56:47 +00:00Commented Aug 7, 2020 at 17:56
2 Answers 2
According to official Microsoft documentation, Attribute routing becomes a requirement in Asp.net core web API applications.
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.
1 Comment
[ApiController]
attribute.In asp.net core web api and mvc you can specify routing
- Startup.cs in Configure method
- 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();
});