1

我在使用 asp.net VB Context.RewritePath加载样式表CSS 时遇到问题。

我的项目正在开发飞行子域系统。意味着当我们在 abcUser.mydomain.com中输入时,它将从mydomain.com/users/abcUser/default.aspx获取 abcUser 的默认页面,而无需更改地址栏的地址。记住不存在任何物理子域

在我的项目中,如果存在用户名文件夹,那么它会从 /users/<abcUser>/default.aspx 加载默认页面。

现在如果在浏览器中我输入直接路径

例如:www.mydomain.com/users/<abcUser>/default.aspx

然后它加载css样式表,但如果我输入这样的路径:

例如:abcUser.mydomain.com

然后它加载我的 default.aspx 页面但不加载 css 文件

  • 这是 Global.asax Application_BeginRequest 代码:

.

If Directory.Exists(Server.MapPath("~/users/" & parameters(i))) Then
   Context.RewritePath("/users/" & parameters(i) & "/default.aspx", False)                    
    Return
Else
    Context.RewritePath("/error.aspx")
    Return
End If

Parameters(i) 变量包含在浏览器中作为子域输入的值,例如:abcUser。

  • 这是我的文件夹结构:

在此处输入图像描述

  • 这是我的 default.aspx 页面代码:

    <link href="StyleSheet.css" rel="stylesheet" />
    

额外细节:我为 microsoft.aspnet.friendly.urls LINK安装了新的 ASP.NET 和 Web Tools 2012.2 更新。它正在按承诺工作,我所有的新旧网页现在都很友好。我的项目是asp.net 4 webform iis7

Global.asax 代码:

  Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    Dim fullHostPath As String = Request.Url.ToString()
    Dim url As New System.Uri(fullHostPath)
    Dim fullDomain As String = url.Host
    Dim parameters() As String = fullDomain.Split(".")
    Dim originalPath As String = HttpContext.Current.Request.Path.ToLower()
    '   

    For i As Integer = 0 To parameters.Length - 1
        If parameters(i) = "localhost" Or parameters(i) = "abc" Then
            'if User enter www.abc.com
            parameters(i) = 0
            Return
        End If
        If parameters(i) = "www" Then
            'if User enter WebName with "www" eg: www.jasbir.abc.com
            'i+=1 gives the next array value, next array is the user name in "fulldomain" variable
            i += 1
            GlobalUserNameVar = parameters(i)   ' get current subdomain name and store for CSS
            If parameters(i) <> "abc" Then
                If originalPath.Contains("/dashboard") And Directory.Exists(Server.MapPath("~/users/" & parameters(i))) Then
                    'check is full path contains "/dashboard" keyword if yes then move to this:-
                    Context.RewritePath(originalPath.Replace("/dashboard", "~/dashboard"), False)
                    Return
                ElseIf originalPath.Contains("/profile") And Directory.Exists(Server.MapPath("~/users/" & parameters(i))) Then
                    'check is full path contains "/profile" keyword if yes then move to this:-
                    Context.RewritePath(originalPath.Replace("/profile", "/users/" & parameters(i) & "/profile"), False)
                    Return
                ElseIf Directory.Exists(Server.MapPath("~/users/" & parameters(i))) Then
                    'check user named directory exists or not if yes then do this:-
                    HttpContext.Current.Server.TransferRequest("/users/" & parameters(i) & "/default.aspx", False)
                    Return
                Else
                    Context.RewritePath("/error.aspx")
                    Return
                End If
            Else
                Return
            End If
        End If
        Next

这是 default.aspx 页面代码

<script type="text/javascript" charset="utf-8">
    $(document).ready(function () {
    function oGod(textboxID, NewValue, textboxUserName) {
        var resultData;

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "default.aspx/HelloWorld",
            data: '{ "varTextBoxID" : "' + textboxID + '", "varNewData" : "' + NewValue + '", "varUserName":  "' + textboxUserName + '"}',
            dataType: "json",
            async: false,
            success: function (msj) {                    
                resultData = msj.d;
                return resultData;
            },
            error: function (e) {                    
                resultData = "error";                   
                return resultData;
            }

        });           

        return resultData;
    }

default.aspx.vb 代码

<WebMethod()> _
Public Shared Function HelloWorld(varTextBoxID As String, varNewData As String, varUserName As String)
    Dim tempData As String = Nothing

    If varTextBoxID = "edit_main_contents" Then
        tempData = UpdateHouseDatabase(varTextBoxID, varNewData, varUserName)
    End If
    If varTextBoxID = "edit_second_contents" Then
        tempData = UpdateHouseDatabase(varTextBoxID, varNewData, varUserName)
    End If
    If varTextBoxID = "user_ID" Then
        tempData = varNewData
    End If

    Return tempData
End Function
4

1 回答 1

1

我最终使用了Server.TransferRequest. 使用此方法时,问题似乎并未显现。我不知道为什么...

于 2013-11-10T17:14:38.777 回答