i want to separate Route config from startup in asp.net core? by default in .net core:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
Right click your web project -> Select Properties -> Select the Debug tab on the left -> Then edit the 'Launch Url' field to set your own default launch url.
To configure services and the request processing pipeline without using a Startup class, call ConfigureServices and Configure convenience methods on the host builder. Multiple calls to ConfigureServices append to one another.
Routing in MVC is easy, here we have Route. Config. cs file in App_Start Folder, You can define Routes in that file, By default route is: Home controller - Index Method.
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. It runs the delegate associated with the selected endpoint.
you can use below code:
public static class RouteConfig
{
public static IRouteBuilder Use(IRouteBuilder routeBuilder)
{
//eg sample for defining Custom route
//routeBuilder.MapRoute("blog", "blog",
// defaults: new { controller = "Home", action = "Index" });
routeBuilder.MapRoute(name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
return routeBuilder;
}
}
and in startup and Configure method:
app.UseMvc(c => RouteConfig.Use(c));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With