Click here to Skip to main content
15,890,399 members
Articles / Web Development / ASP.NET

Adding a base tag to your ASP code dynamically

Rate me:
Please Sign up or sign in to vote.
3.48/5 (10 votes)
21 Jul 2007CPOL 38.4K   21   3
How to dynamically add a base tag to your ASP code.

Introduction

When using relative paths in your HTML code, it is helpful to set the base tag. But that base may be different for each developer on your team, or on stage, QA, or production. Also, when making your URL optimized for search engines, your URL will map to a non-existing path. The best way to solve this is by adding the <base href="..."/> tag to the page header dynamically.

Using the code

The solution is quite simple. First, create a new class called BaseTag that has the following code:

C#
public class BaseTag : HtmlControl
{
    public BaseTag(HttpRequest request): base("base")
    {
        base.Attributes.Add("href", 
            request.Url.Scheme + "://" + 
            request.Url.Authority + 
            request.ApplicationPath + "/");
    }
}

The key part is Url.Scheme that returns the URL scheme: such as HTTP and HTTPS. Url.Authority returns the host and port address, if one is provided. Next is the application path that is often unique to the location of the web app directory. Put them all together with the right string construct, and you get a nice well formed href for the base tag like:

http://someurl:8080/web/

Next, include the following line in the Page_Load method of your *.aspx.cs file:

C#
protected void Page_Load(object sender, EventArgs e)
{
    this.Header.Controls.AddAt(0, new BaseTag(Request)); 
}

The end result is the <base href="http://someurl:8080/web/"/> tag.

I hope this is helpful to you.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAll sorts of problems with base tags [modified] Pin
charliekilian5-Sep-07 10:42
charliekilian5-Sep-07 10:42 
1. Tilde expansion does not work. I'm having trouble finding a general solution for this. For now, I'm hacking around it. Instead of writing code like this:

<a href="~/Members/SomePage.aspx" runat="server">Some Page In The Members Area</a>

I'm writing it like this:

<a href="<%=Request.ApplicationPath %>/Members/SomePage.aspx">Some Page In The Members Area</a><br />


Note that there are two changes: First, the replacement of the ~ with the big long irritating <%...%> section, and -- this part is very important -- removing the runat="server" from the anchor tag.

So that part I can live with, I guess, even though I'm very irritated by it and would like a better solutions.

2. CSS files included in the tag get rewritten. In my master page, I have the following markup:

<link href="style.css" rel="stylesheet" type="text/css" />

Instead, when I'm in the Members folder, it gets output like this:

<link href="../style.css" rel="stylesheet" type="text/css" />

Essentially, ASP.NET is trying to help me by realizing that I'm one folder deeper than I was, so the output page needs to be one level above what it used to be. Except in this case, it's wrong, because I want to have the stylesheet location interpreted as relative to my <base> tag.

For this one, I've found another solution: Move the <link href...> tag outside the <head> tag. It is not W3C compliant, which bugs me, but it stops ASP.NET from providing its ham-fisted "help."

Hope this helps someone someday. Smile | :)
Questionnot working for master page and control ? Pin
freddoo31-Jul-07 3:24
freddoo31-Jul-07 3:24 
GeneralGetLeftPart() Pin
grundt22-Jul-07 7:07
grundt22-Jul-07 7:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.