Смысл в следующем:
При обращении к адресу http://site/dir приложение получает адрес http://site/Default.aspx?n=dir
Дальше все обрабатывается и т.д.
Проблема в том, что для работы решения необходимо создавать фактически каталоги в приложении IIS с необходимыми именами и в них кидать заглушку Default.aspx (пустой файл). Иначе управление не передается модулю подмены, а вылетает ошибка 404
Можно ли решить задачу без создания каталогов-заглушек?
- Код: Выделить всё
Imports System.Collections.Specialized
Imports System.Web
Public Class ParametersClass
Private _Params As NameValueCollection
Private _InitialUrl As String
Sub New()
Me._Params = New NameValueCollection
Me._InitialUrl = String.Empty
End Sub
Sub New(ByVal param As NameValueCollection, ByVal url As String)
Me._InitialUrl = url
Me._Params = New NameValueCollection(param)
End Sub
Property [Params]() As NameValueCollection
Get
Return Me._Params
End Get
Set(ByVal value As NameValueCollection)
Me._Params = value
End Set
End Property
Property [InitialUrl]() As String
Get
Return Me._InitialUrl
End Get
Set(ByVal value As String)
Me._InitialUrl = value
End Set
End Property
Shared ReadOnly Property [Current]() As ParametersClass
Get
If HttpContext.Current.Items.Contains("RequestRewriterParameters") Then
Return CType(HttpContext.Current.Items("RequestRewriterParameters"), ParametersClass)
Else
Return New ParametersClass
End If
End Get
End Property
End Class
Public Class UrlRewriter : Implements IHttpModule
Public Const CONST_RequestRewriterCallDir01 As String = "requestrewritercalldir01"
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
Public Sub Init(ByVal context As System.Web.HttpApplication) Implements IHttpModule.Init
AddHandler context.BeginRequest, AddressOf Me.Application_BeginRequest
AddHandler context.PreRequestHandlerExecute, AddressOf Me.Application_PreRequestHandlerExecute
End Sub
Private Sub Application_BeginRequest(ByVal source As Object, ByVal e As EventArgs)
Dim _Application As HttpApplication = CType(source, HttpApplication)
Dim _Path As String = _Application.Context.Request.Path.ToLower
Dim _PathItems() As String = _Path.Split("/"c)
Dim _PathItemsCount As Integer = _PathItems.GetUpperBound(0)
Dim _CallName As String = _PathItems(_PathItemsCount)
Dim _ParamCallDir01 As String = CONST_RequestRewriterCallDir01 & "=" & _PathItems(_PathItemsCount - 1)
Dim _QueryString As String = _Application.Context.Request.QueryString.ToString
_Application.Context.Items.Add("OriginalUrl", _Application.Context.Request.RawUrl)
If _QueryString.Length > 0 Then
_ParamCallDir01 = "&" & _ParamCallDir01
End If
_QueryString = "?" & _QueryString & _ParamCallDir01
_Application.Context.RewritePath("~/" & _CallName & _QueryString)
End Sub
Private Sub Application_PreRequestHandlerExecute(ByVal source As Object, ByVal e As EventArgs)
Dim _Application As HttpApplication = CType(source, HttpApplication)
If (TypeOf _Application.Context.CurrentHandler Is UI.Page) And (Not _Application.Context.CurrentHandler Is Nothing) Then
Dim _Page As System.Web.UI.Page = CType(_Application.Context.CurrentHandler, UI.Page)
AddHandler _Page.PreInit, AddressOf Me.Page_PreInit
End If
End Sub
Protected Sub Page_PreInit(ByVal source As Object, ByVal e As EventArgs)
If HttpContext.Current.Items.Contains("OriginalUrl") Then
Dim _Path As String = CType(HttpContext.Current.Items("OriginalUrl"), String)
Dim _Cont As New ParametersClass(HttpContext.Current.Request.QueryString, _Path)
HttpContext.Current.Items("RequestRewriterParameters") = _Cont
If _Path.IndexOf("?") = -1 Then
_Path &= "?"
End If
HttpContext.Current.RewritePath(_Path)
End If
End Sub
End Class