Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How separate Route config from startup in asp.net core?

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?}");
        });
like image 726
mostafa mo Avatar asked Aug 13 '18 08:08

mostafa mo


People also ask

How do I set the default route in .NET Core Web API?

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.

Can we configure service and request pipeline without defining startup class?

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.

Which method is used to set start up route file in Routeconfig file?

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.

What is use of UseRouting and UseEndpoints in startup configure 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.


1 Answers

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));
like image 115
amirhamini Avatar answered Sep 28 '22 04:09

amirhamini