首页 文章

如何在Get params中停止asp.net编码?

提问于
浏览
2

我使用以下代码在asp.net中添加一系列对页面的body参数的调用:

uxBodyTag.Attributes["onbeforeunload"] += 
  "ajaxRequest('UnlockQuery.ashx?QueryID=" + queryId.ToString() + 
  "&UserID=" + Session["UserID"].ToString() + "');";

这被呈现为:

<body id="uxBodyTag" onbeforeunload=
    "ajaxRequest('UnlockQuery.ashx?QueryID=176&amp;UserID=11648');">

&amp;意味着我的ashx页面没有检索到正确的变量 - 我如何阻止asp.net这样做?

编辑:

使用Server.UrlEncode给我以下内容:

<body id="uxBodyTag" onbeforeunload=
 "ajaxRequest('UnlockQuery.ashx%3fQueryID%3d179%26UserID%3d11648')%3b">

哪个更糟糕 .

2 回答

  • 4

    在HTML中,&符号需要始终在任何地方进行编码,也需要在属性值中进行编码(显然, <script> 标记的内容是值得注意的例外) . ASP.NET做对了 .

    在实际使用它们之前,浏览器将对属性值进行解码 . 所以 onbeforeunload 属性的文字值为:

    ajaxRequest('UnlockQuery.ashx?QueryID=176&UserID=11648');
    

    而HTML表示需要 &amp; 代替 & . 浏览器通常也会理解编码不良的版本,但SGML解析器会抱怨名为 &UserID 的未知/无效实体 .

  • 2

    您所看到的行为和编码为&amp;是你想要的行为 . 当文本到达你的ajaxRequest函数时,它将再次被取消编码,一切都应该没问题 .

相关问题