Wednesday, April 29, 2015

ASP.Net MVC Using a Base Controller to access the current HTTP request

I'm a big fan of using "base" classes. If I'm working with an ASP.Net web project, I have a BasePage class that all content pages inherit from. If I'm working with a model layer, I have a BaseModel class that all model objects inherit from. If I'm working with a data layer, I have a BaseDB class that all data layer classes inherit from. So when it comes to MVC and dealing with controllers it's no different for MVC controllers or API controllers.

In this example I define a BaseController class that inherits from System.Web.Http.ApiController. This example also shows you how you can access the System.Web.HttpRequest object for the current HTTP request to access information like QueryString, IP Address, URL information, UserAgent information, browser information, and more.
 Public Class BaseController  
   Inherits System.Web.Http.ApiController  

Then you can expose whatever properties or methods you want in your BaseController class.
   Public ReadOnly Property QueryString() As String  
     Get  
       Try  
         Return DirectCast(Request.Properties("MS_HttpContext"), HttpContextBase).Request.QueryString.ToString  
       Catch ex As Exception  
         Return String.Empty  
       End Try  
     End Get  
   End Property  
   
   Public ReadOnly Property IPAddress() As String  
     Get  
       Try  
         Return DirectCast(Request.Properties("MS_HttpContext"), HttpContextBase).Request.UserHostAddress  
       Catch ex As Exception  
         Return String.Empty  
       End Try  
     End Get  
   End Property  
   
   Public ReadOnly Property UrlRequest() As String  
     Get  
       Try  
         Return DirectCast(Request.Properties("MS_HttpContext"), HttpContextBase).Request.Url.AbsoluteUri  
       Catch ex As Exception  
         Return String.Empty  
       End Try  
     End Get  
   End Property  
   
   Public ReadOnly Property RawUrl() As String  
     Get  
       Try  
         Return DirectCast(Request.Properties("MS_HttpContext"), HttpContextBase).Request.RawUrl  
       Catch ex As Exception  
         Return String.Empty  
       End Try  
     End Get  
   End Property  
   
   Public ReadOnly Property UrlReferrer() As String  
     Get  
       Try  
         Return DirectCast(Request.Properties("MS_HttpContext"), HttpContextBase).Request.UrlReferrer.AbsoluteUri  
       Catch ex As Exception  
         Return String.Empty  
       End Try  
     End Get  
   End Property  
   
   Public ReadOnly Property UserAgent() As String  
     Get  
       Try  
         Return DirectCast(Request.Properties("MS_HttpContext"), HttpContextBase).Request.UserAgent  
       Catch ex As Exception  
         Return String.Empty  
       End Try  
     End Get  
   End Property  
   
   Public ReadOnly Property Crawler() As Boolean  
     Get  
       Try  
         Return DirectCast(Request.Properties("MS_HttpContext"), HttpContextBase).Request.Browser.Crawler  
       Catch ex As Exception  
         Return False  
       End Try  
     End Get  
   End Property  

This same concept also works for System.Web.Mvc.Controller.
 Public Class BaseController  
   Inherits System.Web.Mvc.Controller  

In the case of System.Web.Mvc.Controller, we can access the current HTTP request directly from the Request object, without needing to look at MS_HttpContext.
     Public ReadOnly Property QueryString() As String  
       Get  
         Try  
           Return Request.QueryString.ToString  
         Catch ex As Exception  
           Return String.Empty  
         End Try  
       End Get  
     End Property  
   
     Public ReadOnly Property IPAddress() As String  
       Get  
         Try  
           Return Request.UserHostAddress  
         Catch ex As Exception  
           Return String.Empty  
         End Try  
       End Get  
     End Property  
   
     Public ReadOnly Property UrlRequest() As String  
       Get  
         Try  
           Return Request.Url.AbsoluteUri  
         Catch ex As Exception  
           Return String.Empty  
         End Try  
       End Get  
     End Property  
   
     Public ReadOnly Property RawUrl() As String  
       Get  
         Try  
           Return Request.RawUrl  
         Catch ex As Exception  
           Return String.Empty  
         End Try  
       End Get  
     End Property  
   
     Public ReadOnly Property UrlReferrer() As String  
       Get  
         Try  
           Return Request.UrlReferrer.AbsoluteUri  
         Catch ex As Exception  
           Return String.Empty  
         End Try  
       End Get  
     End Property  
   
     Public ReadOnly Property UserAgent() As String  
       Get  
         Try  
           Return Request.UserAgent  
         Catch ex As Exception  
           Return String.Empty  
         End Try  
       End Get  
     End Property  
   
     Public ReadOnly Property Crawler() As Boolean  
       Get  
         Try  
           Return Request.Browser.Crawler  
         Catch ex As Exception  
           Return False  
         End Try  
       End Get  
     End Property  

Now when you create your individual Web or API controllers, you can inherit from the BaseController and access the exposed properties or methods easily.

Matt Pavey is a Microsoft Certified software developer who specializes in ASP.Net, VB.Net, C#, AJAX, LINQ, XML, XSL, Web Services, SQL, jQuery, and more. Follow on Twitter @matthewpavey

0 comments: