我有以下一行aspx链接需要进行编码:
Response.Redirect("countriesAttractions.aspx?=");
我尝试了以下方法:
Response.Redirect(Encoder.UrlPathEncode("countriesAttractions.aspx?="));
这是我尝试过的另一种方法:
var encoded = Uri.EscapeUriString("countriesAttractions.aspx?=");
Response.Redirect(encoded);
两种重定向方式都不会对URL进行编码:
http://localhost:52595/countriesAttractions?=
我尝试了第三种方法:
Response.Redirect(Server.UrlEncode("countriesAttractions.aspx?="));
这次编码的是URL本身:
http://localhost:52595/countriesAttractions.aspx%3F%3D
然而,我从UI界面得到了一个错误提示:
HTTP Error 404.0 Not Found
The resource you are looking for has been removed, had its name changed, or
is temporarily unavailable.
Most likely causes:
-The directory or file specified does not exist on the Web server.
-The URL contains a typographical error.
-A custom filter or module, such as URLScan, restricts access to the file.
此外,我还想编码另一种涉及会话字符串解析的URL:
Response.Redirect("specificServices.aspx?service=" +
Session["service"].ToString().Trim() + "&price=" +
Session["price"].ToString().Trim()));
我尝试将编码方法包含到上面的代码中:
Response.Redirect(Server.UrlEncode("specificServices.aspx?service=" +
Session["service"].ToString().Trim() + "&price=" +
Session["price"].ToString().Trim()));
我使用的上述编码方法显示了与我以前使用的服务器URL编码方法相同的结果。我不确定如何在不出现错误的情况下正确地对URL进行编码。
此外,还可以使用CommandArgument对URL进行编码:
Response.Redirect("specificAttractions.aspx?attraction=" +
e.CommandArgument);
我已尝试了以下编码:
Response.Redirect("specificAttractions.aspx?attraction=" +
HttpUtility.HtmlEncode(Convert.ToString(e.CommandArgument)));
但是它并没有起作用。
有没有办法在不收到这种错误的情况下对URL进行编码? 我希望输出结果类似于我的第二个结果,但是我想看到页面本身而不是错误页面。
我尝试了stackoverflow上找到的其他方法,例如自编码方法,但那些方法也没有起作用。 在这种情况下,我正在使用AntiXSS类库进行尝试,所以如果能够得到使用AntiXSS库的解决方案,那就太好了。 我需要将URL编码作为我的学校项目的一部分,因此如果我可以得到解决方案,那就太好了。谢谢。
countriesAttractions.aspx?=
。第二个是Response.Redirect("specificServices.aspx?service=" + Session["service"].ToString().Trim() + "&price=" + Session["price"].ToString().Trim()));
,但您从未显示任何会话_值_是什么。第三个是使用Response.Redirect("specificAttractions.aspx?attraction=" + HttpUtility.HtmlEncode(Convert.ToString(e.CommandArgument)));
。" - ADyson