styleSheetTheme and full url

Posted on 2009-02-22
Last Modified: 2012-05-06

I use style sheets in ASP.NET application <pages styleSheetTheme="t_native">
When I open web page in browser I see that

<link href="App_Themes/t_native/main.css" type="text/css" rel="stylesheet" />

added to web page. Everything is correct except that I need to use full URL of css file, now it is "App_Themes/t_native/main.css", I need to have ""

How to do that?
Question by:Tomazaz
    LVL 12

    Expert Comment

    If you have
       <link href="App_Themes/t_native/main.css" type="text/css" rel="stylesheet" />
    the page that contains this code will take the context and load the style from the location. You never need to use a full path.  Even if you want to use then,
       <link href="" type="text/css" rel="stylesheet" />

    I think it is advisable to use the first one, as even if the web address changes, you never need to worry about the path.

    Author Comment

    I need to use full url as I asked because I am rewriting url dynamically in my application.
    So my actual url of applications look like

    and in this case relative/absolute URLs do not works. I need to use full URL.
    LVL 12

    Expert Comment

    I am not sure if this works, just try it out.

    1. Have a Literal control, as <asp:Literal ID="litStyle" runat="server"><asp:Literal>
    2. In the Code file in wherever method you want just assign the value of link to the text property as
    litStyle.Text = "<link href=\"\" type=\"text/css\" rel=\"stylesheet\" />";
    LVL 15

    Expert Comment

    by:Praveen Venu
    why can not put the tag

    directly in the ASPX page?

    Author Comment

    I can but I will loose CSS in design time, also I do not want to use hack, ASP.NET offer style sheet solution.
    LVL 12

    Expert Comment

    Yes obviously.  Coz, the full url will always work, even if you do a URL rewrite, when you put it directly as I said in the first post.

    Accepted Solution

    I just solved problem by myself. The solution is attached. Please admin refund my points.
      <browser refID="Default">
    		<adapter controlType="System.Web.UI.HtmlControls.HtmlLink" adapterType="LinkRewriterControlAdapter" />
    Imports Microsoft.VisualBasic
    Public Class LinkRewriterControlAdapter
        Inherits System.Web.UI.Adapters.ControlAdapter
        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            MyBase.Render(New RewriteLinkHtmlTextWriter(writer))
        End Sub
    End Class
    Public Class RewriteLinkHtmlTextWriter
        Inherits HtmlTextWriter
        Sub New(ByVal writer As HtmlTextWriter)
            Me.InnerWriter = writer.InnerWriter
        End Sub
        Sub New(ByVal writer As System.IO.TextWriter)
            MyBase.InnerWriter = writer
        End Sub
        Public Function GetBaseURL() As String
            Dim url As String
            url = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath.TrimEnd("/") + "/"
            If (url.EndsWith("/")) Then
                Return url.Remove(url.LastIndexOf("/"))
                Return url
            End If
        End Function
        Public Overrides Sub WriteAttribute(ByVal name As String, ByVal value As String, ByVal fEncode As Boolean)
            ' If the attribute we are writing is the "action" attribute, and we are not on a sub-control, 
            ' then replace the value to write with the raw URL of the request - which ensures that we'll
            ' preserve the PathInfo value on postback scenarios
            If (name = "href") Then
                Dim Context As HttpContext
                Dim sStylesheetName As String
                Dim iSlashPos As Integer
                Dim contextItemKey As String            
                Context = HttpContext.Current
                contextItemKey = value + "HrefAlreadyWritten"
                If Context.Items(contextItemKey) Is Nothing Then
                    sStylesheetName = value
                    iSlashPos = value.LastIndexOf("/")
                    'If (iSlashPos >= 0) Then sStylesheetName = value.Substring(iSlashPos)
                    value = GetBaseURL() + "/" + sStylesheetName
                    Context.Items(contextItemKey) = True
                End If
            End If
            MyBase.WriteAttribute(name, value, fEncode)
        End Sub
    End Class

    Open in new window


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now