Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Customizing Routes In MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 302
    Comment on it

    Customizing Routes :-

    In MVC application we are using routing for mapping the URL structure. But sometimes we have situation to change the URL structure according to the requirement. For achieving the requirement, we are creating custom routes.

    Example: - Just Imagine that you want to create a Content Posting application and you want to the route request like:

    http://localhost:44300/Content/PostContent/10-09-2015

    The first step is to create an MVC application with your project name. And add Controller with names like ContentController and add ActionsMethod with name PostContent and InfoMessage that the code will look like below:

    public class ContentController : Controller
        {     
            public string PostContent(DateTime? currentDate)
            {
                return "You wants content posting on Date:=" + currentDate.ToString();
            }
    
            public ActionResult InfoMessage()
            {
                ViewData["Message"] = "Call  the InfoMessage Action When Get Http request Occurs";
                return View();
            }
        }
    

    Now go the RouteConfig.cs file and add the following code into the Register Routes method.

    //Simple Custom Route with out Constraints
    routes.MapRoute(
                    "MethodwithoutConstraint",
                    "PostContent/{currentDate}",
                    new { Controller = "Content", action = "PostContent", });
    

    Route config file register on global.asax file and available on App_Start folder. Above code used to add Custom routes and it will match the URL like,

    http://localhost:44300/Content/PostContent/10-09-2015

    Route Constraints:--

    When you create custom route for application so we can also include route constraints. Basically the constraints are used to restrict the requests to match routes. Three basic types of constraints are below:--

    1. Regular Expression Constraints
    2. HttpMethod Constraints
    3. CatchAll Values

    Regular Expression Constraints:

    You can use Regular expression to check the any pattern like special characters, date, time, currency, etc. Using Regular expression constraints you can prevent the invalid URL requests.

    http://localhost:44300/Content/PostContent/10-09-2015

    In above url the date is added to url request. If you want not allowed url request without a date like (https://localhost:44300/Content/PostContent/Index). So you can add regular expression on your Route method like below code:---

    //Custom Route With Regular Expression Constraints
                routes.MapRoute(
                    "PostContent",
                    "PostContent/{currentDate}",
                    new { Controller = "Content", action = "PostContent" }, 
            new { entryDate = @"\d{2}-\d{2}-\d{4}" });
    

    HTTP Method Constraints: You can also match url with any type of HTTP request like Get, Post, Delete, etc. If you want to allowuser to access application when only Get request comes but not allowed Post, Delete etc. Request.You can use AcceptVerbs instead of HttpMethod constraints.

    For example, InfoMessage Action only executes when an HTTP GET request comes. For that, you can add below code in Route Method in RouteConfig file.

    //Custom Route With HttpMethod Constraint
                routes.MapRoute(
                    "HttpMethodConstraintRoute",
                    "PostContent/InfoMessage",
                    new { Controller = "Content", action = "InfoMessage" }, 
            new { method = new HttpMethodConstraint("GET") });
    

    Catch All Routes: Basically url matches with some segments, If you have n numbers of segments in url requests, then you need to create a route that will catch all parameters.

    For creating catch all routes you need to add one more action in your Content controller like :--

           public string Index(string AllParameters)
           {
              string[] UrlParameters= AllParameters.Split('/');
              return "Url Passing Values:" + String.Join(",", AllParameters);
           }
    

    For catching the all url request parameters you need to add below code in the route config file.

    //Custom Route Catch-All  Routes
          routes.MapRoute(
               "CatchAllRoute",
                "Content/{*AllValues}",
                new { Controller = "Content", action = "Index" });
    

    The above route will match the following url like:--- http://localhost:44300/Content/Index/1/2/3/ http://localhost:44300/Content/Index/a/b/c

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: