Stylesheet CSS not loading with Context.RewritePat

2019-05-30 09:31发布

问题:

I am getting problem in loading stylesheet CSS with asp.net VB Context.RewritePath.

My project is working on fly subdomain system. Means when we enter this in abcUser.mydomain.com then it will get default page of abcUser from mydomain.com/users/abcUser/default.aspx, without changing address bar's address. remember there is no any physical subdomain exists.

in my project if user named folder exists then it load the default page from /users/< abcUser>/default.aspx.

now if in browser i enter direct path

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

then it load css stylesheet, but if i enter path like this:

eg: abcUser.mydomain.com

Then it load my default.aspx page but not loading css file

  • This is Global.asax Application_BeginRequest code:

.

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) variable contains the value entered in browser as subdomain eg: abcUser.

  • This is my folder structure:

  • This is my default.aspx page code:

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

Extra Detail: i installed new ASP.NET and Web Tools 2012.2 Update for microsoft.aspnet.friendly.urls LINK. and it is working as promised, my all new new and old web pages are now friendly. my project is asp.net 4 webform iis7

Global.asax code:

  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

This is default.aspx page code

<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 code

<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

回答1:

I ended up using a Server.TransferRequest. The problem doesn't seem to manifest itself when this method is used. I don't know why...