mvc

Static MVC.NET URL segments explained-Trickcode

Static MVC.NET URL Segments Explained,MVC.NET URL ,URL pattern ,Controller,ProductLine
Share it:
Static MVC.NET URL segments explained

Static MVC.NET URL Segments Explained

Static URL segments are used when URL patterns don’t require having all segments to be variables.
 For instance, you may have Areas of your application with the same Controllers and Actions such as http://site.com/Public/Group/List and http://site.com/Admin/Group/List
We don’t care about Public or Admin being a variable and need to direct our MVC to ignore these two segments

This can be achieved with the following code

 public static void RegisterRoutes(RouteCollection routes)

        {

             routes.MapRoute("", "Public/{controller}/{action}",  new { controller = "Group", action = "List" });

        }

We can also create a URL pattern that is a combination of Static and Variable elements.


public static void RegisterRoutes(RouteCollection routes)

        {

            routes.MapRoute("", "X{controller}/{action}");

        }



This mixed pattern is useful when we want to extract Controller and Action from the URL in the following format http://site.com/xGroup/List. The mixed URL pattern will grab Group for Controller and List for Action while ignoring x in the URL address.

There may be times when you rewrite your application and methods being replaced with new methods. However, you would like to preserve your URLs for search engines to work properly. The way to achieve this is by creating a new mixed type of route as shown below:


public static void RegisterRoutes(RouteCollection routes)

{ 

    routes.MapRoute("UserSchema", "Group/{action}", new { controller = "ProductLine" });

 }

The above-mentioned route will help you handle the new Controller called ProductLine in place of an older Method called Group. The group is now static segment and will be ignored as a controller. the product line will be new Controller with original action being used.
Share it:

mvc

Post A Comment:

0 comments: