PBMining

Searching...
Monday, June 11, 2012

Constraining Routes in Asp.net MVC 3

3:48 AM

In my previous post i show you how to control URL Request by using routing pattern.when route is matched and values of different segment is extract from Url next thing is to control over that values.we can do it by putting regular expression in constrain parameter of route definition.

Constraining Route using regular expression

        routes.MapRoute("R15", "{controller}/{action}/{id}",
                                     Nothing,
                                     New With {.controller = "^R.*", .action = "^Index$|^About$"})


    

In above Route definition we add constrain for controller name and action name.it says that if request is only handle by this route if Controller name start with R and action name should either Index OR About. you can define constrain for each segment as you want.

Constraining route Using HTTP Methods

you can also control request using Specific HTTP Methods.if you want that some route only handle HTTP GET OR POST request.

routes.MapRoute("R14", "{controller}/{action}/{id}",
                               Nothing,
                               New With {.httpmethod = New HttpMethodConstraint("GET")})

In above code we add constraint for HTTP GET and we use class HttpMethodConstraint with Parameter GET it define that this route only handle GET Http request.

Constraining Route with Custom Constraint

All above ways for constraining route is work with built in MVC mechanism.what if we want to define our own criteria to handle request in route? as solution of this you can create your own Custom constrain class and pass it as constraint parameter. Routing system provide IRouteConstraint Interface that we are going to use for creating CustomConstraint.

Public Class CustomConstraint
    Implements IRouteConstraint

    Private _ua As String
    Sub New(ByVal userAgent As String)
        _ua = userAgent

    End Sub


    Public Function Match(httpContext As System.Web.HttpContextBase, route As System.Web.Routing.Route, parameterName As String, values As System.Web.Routing.RouteValueDictionary, routeDirection As System.Web.Routing.RouteDirection) As Boolean Implements System.Web.Routing.IRouteConstraint.Match
        Return httpContext.Request.UserAgent IsNot Nothing AndAlso httpContext.Request.UserAgent.Contains(_ua)

    End Function
End Class
    

we create class that implements IRouteConstraint Interface this interface expose abstract method Match.this method call when request comes to route and route execute the constraint. we also create constructor for passing the criteria to constraint class that is going to match with incoming request data. we are going to check for UserAgent of incoming request,is it match with the value that we passed in constructor.if so then route will handle that request. and below we define the route with and pass IE as UserAgent. so this route will handle any request by Internet Explorer browser.

    routes.MapRoute("R15", "{controller}/{action}/{id}",
                                 Nothing,
                                 New With {.customConstraint = New CustomConstraint("IE")})

0 comments:

Post a Comment

Write your review about this post.