1

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:

enter image description here

  • 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
JSB
  • 215
  • 5
  • 16
  • Did you find a solution to the problem? – Kees C. Bakker Nov 07 '13 at 12:24
  • no i try my best but unfortunately I was not able to find solution. My project work stop because of this problem. if you ll find a solution then please share it with me. I ll appreciate – JSB Nov 10 '13 at 11:50
  • I've used a `Server.TransferRequest` to solve the problem, but I don't know why. – Kees C. Bakker Nov 10 '13 at 17:13
  • I'm not seeing the method of the global.asax you're overriding. I've implemented it on the `Authenticate_Request`. Note: I'm a C# developer :P – Kees C. Bakker Nov 12 '13 at 15:25
  • Ok, i implemented upper code in Application_BeginRequest section. – JSB Nov 12 '13 at 20:20
  • for a clear demo of project, i updated it for you on a server. try 3 address in browser and each will give you different output for same file. First address is this www.bhinderblink.com/users/jasbir/default.aspx it will load CSS perfect, but i do not want to access file like this. SECOND ADDRESS IS: without "www", jasbir.bhinderblink.com, This is with Context.RewritePath method, it ll load users/jasbir/default.aspx file without CSS. THIRD ADDRESS IS: with "www", www.jasbir.bhinderblink.com, This is with Server.TransferRequest method as you told me,it shows a server error and not loading a page – JSB Nov 12 '13 at 20:54
  • 1
    I also needed to add the following to my web.config to parse CSS through the global.asax: ` ` – Kees C. Bakker Nov 13 '13 at 09:55
  • So my StyleSheets are also "virtual" like /users/KeesCBakker/default.css – Kees C. Bakker Nov 13 '13 at 09:56
  • in my project runAllManagedModulesForAllRequests was already true, i change it to false eg:- ........ Now CSS working even with Context.RewritePath method – JSB Nov 13 '13 at 14:54
  • 1
    Thankx... Kees C. Bakker for helping.. I really appreciate your help – JSB Nov 13 '13 at 15:53
  • Hello Kees, could you please help me to debug this issue too. As you last time helped me to fix css loading with Context.RewritePath and same issue is with jquery function. When i am running direct link eg http:\\bhinderblink.com\users\jasbir\default.aspx and clicking on text to edit then it is saving data to database. but it is not working if i use address http:\\jasbir.bhinderblink.com\ jquery shows error message....the thing is css and images and jeteditable.js all are loading without any issue. but it is not trigering same page webmethod function. any suggestion is welcomed – JSB May 26 '14 at 03:57
  • I updated webmethod code and jquery code too in this question. – JSB May 26 '14 at 03:59

1 Answers1

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

Kees C. Bakker
  • 32,294
  • 27
  • 115
  • 203
  • Yep... worked perfectly. Just use the `HttpContext.Current.Server.TranferRequest` instead of the `RewritePath`. The parameters are almost the same. – Kees C. Bakker Nov 11 '13 at 18:37
  • The thing is... I'm rewriting all requests in my global.asax. – Kees C. Bakker Nov 11 '13 at 18:38
  • i try it but my side it is not working, ending with a server error.. can you give me your email id so i ll send you my global.asax file. – JSB Nov 12 '13 at 04:05