【发布时间】:2009-01-06 17:49:31
【问题描述】:
问题
我正在尝试构建一个快速简单的 ASP.NET 页面,该页面使用元重定向将用户重定向到新 URL。唯一的麻烦是我还需要传递当前请求的 GET 值。我在使用 HtmlMeta 对象的代码中找到了一种以编程方式执行此操作的方法。但是,我想避免使用后面的代码,直接把这段代码放到 ASPX 页面中。
这是我目前所拥有的:
<!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>
但是,这会吐出以下元标记:
<meta http-equiv="refresh" content="10;url=http://contact.test.net/main.aspx?<%=Request.QueryString.ToString()%>" />
那么有什么方法可以转义属性,以便 ASP.NET 代码实际执行?
解决方案 1
目前,我通过从 HTML 属性中删除引号来解决我的问题。从而使元标记如下:
<meta http-equiv="refresh" content=10;url=http://contact.test.net/main.aspx?<%=Request.QueryString.ToString()%> />
虽然这解决了这个问题,但我很好奇是否有人知道更正确的方法,我可以逃避 HTML 属性的文字引号。
解决方案 2(最终选择的解决方案)
根据 Scott 非常感谢的建议,我决定继续从后面的代码中执行此操作。对于任何好奇这是如何实现的人:
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
谢谢,
克里斯
【问题讨论】: