ASP.NET MVC > Route

Browser constraint check in route in ASP.NET MVC

How to perform a browser constraint check in the route and redirect the user to browser specific page?


To demonstrate this example, we need to create few pages. Please follow below steps carefully without missing any steps. All steps are mandatory.

APP_START/ROUTECONFIG.CS

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        
        // Custom constraint - executes only when the request is made from
Google chrome browser
            routes.MapRoute("ConstraintCustomChrome",
"BrowserCheck/{action}/{id}/{*catchall}",
                new { controller = "RoutingStuffs", action = "Index", id =
UrlParameter.Optional },
                new
                {
                    customConstraint = new Utility.BrowserDetectConstraint("Chrome")
                }, new[] { "MVCTraining5.Controllers.OtherFolder" });
            
            routes.MapRoute("ConstraintCustom",
"BrowserCheck/{action}/{id}/{*catchall}", 
                new { controller = "RoutingStuffs", action = "Index", id =
UrlParameter.Optional },
                new[] { "MVCTraining5.Controllers" });

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id =
UrlParameter.Optional }
            );
       }
    
    }  

In the above code snippet notice that we have moved the default route mentioned in the RouteConfig.cs file to the last so that this do not interrupt with the custom route defined by us.

In the first route, notice that till 3rd parameter everything is normal (we have already seen these parameters earlier). The 4th parameter is the constraint defined by us that exists in the Utility namespace (Utility folder inside the root of the application, we will see this next) and the 5th parameter is the namespace from which RoutingStuffs controller should be used for this type of routing (provided the constraint is returning true (we will see it later).

In the second route, we have skipped the 4th parameter and specified the namespace from which RouteStuffs controller should be used for this routing (we will define two different controller with the same name in different namespace; do not worry, we are going to see them all).

Now let’s create the custom constraint (BrowserDetectConstraint.cs), create a new class file and write below code.

/UTILITY/BROWSERDETECTCONSTRAINT.CS

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;

namespace MVCTraining5.Utility
{
    public class BrowserDetectConstraint : IRouteConstraint
    {
        string _userAgent = string.Empty;

        public BrowserDetectConstraint(string userAgent)
        {
           _userAgent = userAgent;
        }

        public bool Match(HttpContextBase httpContext, Route route, string
parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            return httpContext.Request.UserAgent != null
                && httpContext.Request.UserAgent.Contains(_userAgent);
        }
    }
}

Notice that the class file is implementing IRouteConstraint interface that forces us to implement Match method which returns true or false based on user agent coming in to this constraint in the constructor.

Now let’s create controllers

/CONTROLLERS/ROUTINGSTUFFSCONTROLLER.CS

public ActionResult BrowserChromeDetech()
 {
     ViewBag.Browser = "This is from /Controller ";
     return View();
 }

Create its view

/VIEWS/ROUTINGSTUFFS/BROWSERCHROMEDETECH.CSHTML

@{
    ViewBag.Title = "BrowserChromeDetech";
}

<h2>View for browser other than Google Chrome Browser</h2>

@ViewBag.Browser

Create another controller with the same name in different namespace. We need to create a folder insider Controllers folder named OtherFolder (we can change the name, if we decided to change the name then we need to also change the namespaces name in our code wherever “OtherFolder” is referred here).

/CONTROLLERS/OTHERFOLDER/ ROUTINGSTUFFSCONTROLLER.CS

public ActionResult BrowserChromeDetech()
   {
       ViewBag.Browser = "This is from /OtherFolder/Controller ";
       
       return
       View("/Views/RoutingStuffs/OtherStuffs/BrowserChromeDetect.cshtml");
}

Create its view in the respective folder.

/VIEWS/ROUTINGSTUFFS/OTHERSTUFFS/BROWSERCHROMEDETECT.CSHTML

@{
         ViewBag.Title = "BrowserChromeDetect";
}

<h2>View for Google Browser</h2>

<p>View from other folder</p>

<p>
       @ViewBag.Browser
</p>

Now run the application and navigate to http://localhost:63087/BrowserCheck/BrowserChromeDetech in the Google Chrome browser and it returns view that is specific to Google chrome browser. Again copy-paste the url to other browser than Google Chrome (like Internet Explorer or FireFox and you should see different view.

How it works?

When we request the above url from Google chrome browser, because of “BrowserCheck” string in the starting of the url (1st fragment in the url), either ConstraintCustomChrome or ConstraintCustom route should execute (see RouteCongif.cs file code above). However in this case, the first one will execute as it has a constraint (4th parameter - BrowserDetectConstraint) and we are passing “Chrome” as its parameter. In the BrowserDetectConstraint, Match method returns true because the user agent will contain “Chrome” word (as the request is coming from Google Chrome browser) that will instruct the ASP.NET MVC framework to go ahead and call the BrowserChromeDetect() action (notice the url 2nd fragment) method of the RoutingStuffs controller from MVCTraining5.Controllers.OtherFolder namespace (notice the 5th paramter).

Now see the BrowserChromeDetect() action method of MVCTraining5.Controllers.OtherFolder.RoutingStuffsController namespace (/Controllers/OtherFolder/). In this action method we are setting some string value to the ViewBag.Browser and then returning the View that is in the Views/RoutingStuffs/OtherStuffs/BrowserChromeDetect.cshtml

Now, if the same url is browsed from different browser other than Google Chrome then the 2nd route from RouteConfig.cs executes as the custom constraint (BrowserDetectConstraint) returns false so even if the url is same but 1st route doesn’t executes. The 2nd route intern call the RoutingStuffs controller from MVCTraining5.Controllers folder that returns the respective view from /Views/RoutingStuffs/BrowserChromeDetect.cshtml (see the code snippet above).

 Views: 11837 | Post Order: 70



Write for us






Hosting Recommendations