regex
regex

Reputation: 3601

Edit HTML Meta Tag w/ ASP.NET

Question


I'm trying to build a quick and easy ASP.NET page that redirects a user to a new URL using a meta redirect. Only trouble is that I need to also pass along the GET values of the current request. I've found a way to do this programatically in the code behind using the HtmlMeta object. However, I'd like to avoid using the code behind and just put this code directly into the ASPX page.

Here is what I have so far:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <meta http-equiv="refresh" content='10;url=http://contact.test.net/main.aspx?<%=Request.QueryString.ToString()%>' />
</head>
</html>


However, this spits out the following meta tag:

<meta http-equiv="refresh" content="10;url=http://contact.test.net/main.aspx?<%=Request.QueryString.ToString()%>" />

So is there any way to escape the attribute so the ASP.NET code actually executes?

Solution 1


For the time being, I have fixed my problem by removing the quotes from the HTML attribute. Thus making the meta tag the following:


<meta http-equiv="refresh" content=10;url=http://contact.test.net/main.aspx?<%=Request.QueryString.ToString()%> />



Although this fixes the issue, I'd be curious if anyone knows of a more correct way to do it where I could escape the literal quotes of the HTML attribute.


Solution 2 (Final Chosen Solution)


Per the much appreciated advise of Scott, I decided to go ahead and do this from the code behind. For anyone who is curious how this was implemented:


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim nRef As String = Request.QueryString("n")
        Dim sRef As String = Request.QueryString("s")

        Dim contentAttrBuilder As New StringBuilder("0;http://contact.cableone.net/main.aspx")
        contentAttrBuilder.Append("?n=")
        contentAttrBuilder.Append(nRef)
        contentAttrBuilder.Append("&s=")
        contentAttrBuilder.Append(sRef)

        Dim metaRedirect As New HtmlMeta()
        metaRedirect.HttpEquiv = "refresh"
        metaRedirect.Content = contentAttrBuilder.ToString()

        Me.Header.Controls.Add(metaRedirect)

    End Sub

Thanks,
Chris

Upvotes: 2

Views: 5531

Answers (3)

Michiel van Oosterhout
Michiel van Oosterhout

Reputation: 23084

Remove the runat="server" attribute from <head>

Upvotes: 0

Jorge Alves
Jorge Alves

Reputation: 1168

Maybe this code inside the head tag will be what you need:

<%= string.Format("<meta http-equiv='refresh' content='10;url=http://contact.test.net/main.aspx?{0}' />", Request.QueryString.ToString()) %>

However, I wouldn't advise you to do it this way. For example, this URL:

http:/mysite.with.metaredirect?<script>alert('hello!!!')</script>

will throw an exception in asp.net if you haven't disabled its security features, and you never know if someone (or even yourself) will turn those off for some other reason.

A code-behind massage of the querystring is strongly advised!

Upvotes: 3

Jorge Alves
Jorge Alves

Reputation: 1168

Have you tried adding the whole meta tag programmatically?
Once you get the to run server-side, add your new tag dynamically on Page_Load() and you can specify all its attributes.

The process is well described here:
http://www.howtoadvice.com/SetMetaTags

Upvotes: 1

Related Questions