【问题标题】:Stylesheet CSS not loading with Context.RewritePath样式表 CSS 未使用 Context.RewritePath 加载
【发布时间】:2013-03-14 13:04:49
【问题描述】:

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

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

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

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

例如:www.mydomain.com/users//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 工具 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

【问题讨论】:

  • 您找到解决问题的方法了吗?
  • 不,我尽力而为,但不幸的是我无法找到解决方案。由于这个问题,我的项目工作停止了。如果您能找到解决方案,请与我分享。我会感激的
  • 我用Server.TransferRequest解决了这个问题,但我不知道为什么。
  • 我还需要将以下内容添加到我的 web.config 以通过 global.asax 解析 CSS:&lt;system.webServer&gt; &lt;modules runAllManagedModulesForAllRequests="true"&gt; &lt;/module&gt; &lt;system.webServer&gt;
  • 谢谢... Kees C. Bakker 的帮助.. 非常感谢您的帮助

标签: asp.net vb.net visual-studio-2010 global-asax


【解决方案1】:

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

【讨论】:

  • 是的......工作得很好。只需使用HttpContext.Current.Server.TranferRequest 而不是RewritePath。参数几乎一样。
  • 问题是......我正在重写我的 global.asax 中的所有请求。
  • 我试了一下,但我这边它不工作,以服务器错误结束。你能给我你的电子邮件 ID,以便我将我的 global.asax 文件发送给你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-01
  • 2012-11-23
  • 2015-04-08
  • 1970-01-01
  • 1970-01-01
  • 2014-03-14
  • 2014-09-07
相关资源
最近更新 更多