I have successfully created an iHttpModule to replace my Global.asax file in many Web Forms applications. But in looking at the Global.asax file in my MVC application, the methods are totally different.
I'm wondering if it is still possible to create this same thing in an MVC app. I know it's not necessary and the Global.asax works just fine. I suppose I just want to have nothing but the web.config in the root directory of my application.
Also, I am putting all of my classes in a separate class library project instead of a folder in my MVC application. Not sure if this makes a difference or not.
Here's what I have currently, but unfortunately my routes are not registering
MVCApplication.vb (Class)
Imports System.Web.Mvc
Imports System.Web.Routing
Imports System.Web
Imports System.Text.RegularExpressions
Public Class MvcApplication : Inherits System.Web.HttpApplication : Implements IHttpModule
#Region "Global Variables/Objects"
Private UrlRegex As New Regex("(http|https)://www\.", RegexOptions.IgnoreCase Or RegexOptions.Compiled)
Private ApplicationContext As HttpApplication
Private BeginRequestEventHandler As EventHandler
Private ErrorEventHandler As EventHandler
#End Region
#Region "Init and Dispose"
Public Overrides Sub Dispose() Implements System.Web.IHttpModule.Dispose
RemoveHandler ApplicationContext.BeginRequest, BeginRequestEventHandler : BeginRequestEventHandler = Nothing
RemoveHandler ApplicationContext.Error, ErrorEventHandler : ErrorEventHandler = Nothing
End Sub
Public Overridable Overloads Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
ApplicationContext = context
AddHandler ApplicationContext.BeginRequest, AddressOf OnBeginRequest
AddHandler ApplicationContext.Error, AddressOf OnError
End Sub
#End Region
Protected Sub OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs)
''# Crap in here about redirecting WWW
End Sub
Protected Sub OnError(ByVal sender As Object, ByVal e As EventArgs)
''# crap in here about logging errors
End Sub
Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
''# MapRoute takes the following parameters, in order:
''# (1) Route name
''# (2) URL with parameters
''# (3) Parameter defaults
routes.MapRoute( _
"Default", _
"{controller}/{action}/{id}", _
New With { _
.controller = "Home", _
.action = "Index", _
.id = UrlParameter.Optional} _
)
End Sub
Sub Application_Start()
AreaRegistration.RegisterAllAreas()
RegisterRoutes(RouteTable.Routes)
End Sub
End Class
web.Config
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true">
<add name="_Global" type="UrbanNow.hClassLib.MvcApplication"/>
</modules>
</system.webServer>