Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


styleSheetTheme and full url

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

I use style sheets in ASP.NET application <pages styleSheetTheme="t_native">
When I open asp.net 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 "http://www.mydomain.com/App_Themes/t_native/main.css" 

How to do that?
Question by:Tomazaz
  • 3
  • 3
LVL 12

Expert Comment

ID: 23708995
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="http://www.mydomain.com/App_Themes/t_native/main.css" 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

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


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

Expert Comment

ID: 23709078
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=\"http://www.mydomain.com/App_Themes/t_native/main.css\" type=\"text/css\" rel=\"stylesheet\" />";
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 15

Expert Comment

by:Praveen Venu
ID: 23709080
why can not put the tag

directly in the ASPX page?

Author Comment

ID: 23709119
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

ID: 23709126
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

Tomazaz earned 0 total points
ID: 23710610
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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

572 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