find and solve ||
Please wait.....

Routing in ASP.NET Core MVC-Dynamic Route

This is achieved by adding Routing middleware to the pipeline and using IRouteBuilder to map URL pattern to a controller and action.ASP.NET Core takes advantage of routing to map incoming requests to respective controller actions.Mapping is done by the routing rules which are defined for the application.In .NET 5 Core controllers use the Routing middleware to match the URLs of incoming requests and map them to actions. Route templates.

  1. All are routing defined in startup code or attributes.
  2. Describe how URL paths are matched to actions.
  3. Are used to generate URLs for links.The generated links are typically returned in responses.

There are two types of routing for action methods:

  1. Convention Based Routing (conventional route)
  2. Attribute-Based Routing (conventional routing)

Conventional Based Routing

Convention based Routes are defined within the Configure method of the Startup.cs class file.Startup.Configure typically has code similar to the following when using conventional routing:

app.UseEndpoints(endpoints =>
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");

Inside the call to UseEndpoints, MapControllerRoute is used to create a single route which route is named default route.

Conventional Routing establishes a convention for the URL path, such that given a template:

Matches a URL path like /Employee/Details/5

  1. First Map to Controller
  2. Second Map to Action
  3. Third Map to an optional id action parameter

Attribute-Based Routing

Conventional routing or Attribute-Base Routing is used with controllers and views. The default route.Conventional Maps URLs by applying routing template directly on the controller and action.

The preceding is an example of a Convention Based Routing . It's called Attribute-Based Routing because it establishes a convention for URL paths:

  1. The first path segment, {controller=ControllerName}, maps to the controller name.
  2. The second segment, {action=ActionName}, maps to the action name.
  3. The third segment, {id?} is used for an optional id. The ? in {id?} makes it optional. id is used to map to a model entity.

This is Is based on the controller and action names only and Isn't based on namespaces, source file locations, or method parameters.

Multiple Routes

This  can be added inside UseEndpoints by adding more calls to MapControllerRoute and MapAreaControllerRoute.The most common usage of this is to achieve the functionality of the default conventional route:

          app.UseEndpoints(endpoints =>
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                //About Us
                    name: "AboutUs",
                    pattern: "about-us",
                    defaults: new { controller = "Home", action = "AboutUs" });
                     name: "TearmsOfUse",
                     pattern: "terms-of-use",
                     defaults: new { Controller = "Home", Action = "TearmsOfUse" });
                //Blog details
                        name: "BlogDetails",
                        pattern: "blog/{value}",
                        defaults: new { Controller = "Home", Action = "BlogDetails" });

Call this url like as given below.

      public class HomeController : Controller{
      //this is called about-us pattern
    public ActionResult AboutUs()  
        {             return View();         }
       //this is called terms-of-use pattern
        public ActionResult TearmsOfUse()         {
            return View();         }
       //this is called blog/something pattern
     public ActionResult BlogDetails(string value)
            return View();

Mahira  khanna

Mahira khanna

I have the skills you need for you company blog, website, or other content materials


Report Response