如何使用 Asp.net 对 URL 进行编码?

dep*_*irl 5 c# asp.net url encoding

我有以下想要编码的 aspx 链接行:

 Response.Redirect("countriesAttractions.aspx?=");
Run Code Online (Sandbox Code Playgroud)

我尝试了以下方法:

 Response.Redirect(Encoder.UrlPathEncode("countriesAttractions.aspx?="));
Run Code Online (Sandbox Code Playgroud)

这是我尝试过的另一种方法:

    var encoded = Uri.EscapeUriString("countriesAttractions.aspx?=");
    Response.Redirect(encoded);
Run Code Online (Sandbox Code Playgroud)

两者都重定向到未编码 URL 的页面:

http://localhost:52595/countriesAttractions?=
Run Code Online (Sandbox Code Playgroud)

我试过这第三种方法:

 Response.Redirect(Server.UrlEncode("countriesAttractions.aspx?="));
Run Code Online (Sandbox Code Playgroud)

这次 url 本身被编码:

http://localhost:52595/countriesAttractions.aspx%3F%3D
Run Code Online (Sandbox Code Playgroud)

但是,我从 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.
Run Code Online (Sandbox Code Playgroud)

另外,我想编码另一种涉及会话字符串解析的 URL:

Response.Redirect("specificServices.aspx?service=" + 
Session["service"].ToString().Trim() + "&price=" + 
Session["price"].ToString().Trim()));
Run Code Online (Sandbox Code Playgroud)

我尝试在上面的代码中包含编码方法的方法:

Response.Redirect(Server.UrlEncode("specificServices.aspx?service=" + 
Session["service"].ToString().Trim() + "&price=" + 
Session["price"].ToString().Trim()));
Run Code Online (Sandbox Code Playgroud)

我使用的上述编码方法显示了我使用以前的服务器 URL 编码方法收到的相同类型的结果。我不确定如何以正确的方式对 url 进行编码而不会出错。

以及使用 CommandArgument 编码 URL:

Response.Redirect("specificAttractions.aspx?attraction=" + 
e.CommandArgument);
Run Code Online (Sandbox Code Playgroud)

我尝试了以下编码:

Response.Redirect("specificAttractions.aspx?attraction=" + 
HttpUtility.HtmlEncode(Convert.ToString(e.CommandArgument))); 
Run Code Online (Sandbox Code Playgroud)

但它没有用。

有什么方法可以对 url 进行编码而不会收到此类错误?我希望输出类似于我的第二个结果,但我想查看页面本身而不是错误页面。

我尝试过在 stackoverflow 上找到的其他方法,例如自编码方法,但这些方法也不起作用。在这种情况下,我将 AntiXSS 类库用于我尝试过的方法,因此如果我可以使用 AntiXSS 库获得解决方案,那就太好了。我需要将 URL 编码为我学校项目的一部分,所以如果我能得到解决方案就太好了。谢谢你。

ADy*_*son 2

您可以使用HttpUtility 类中的UrlEncodeUrlPathEncode方法来实现您的需要。请参阅https://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode(v=vs.110).aspx上的文档

然而,重要的是要理解,您不需要对整个 URL 字符串进行编码。您只需对参数值(可能包含 URL 中无效的任意数据和字符)进行编码。

为了解释这个概念,请在简单的 .NET 控制台应用程序中运行以下命令:

string url = "https://www.google.co.uk/search?q=";
//string url = "http://localhost:52595/specificAttractions.aspx?country=";
string parm = "Bora Bora, French Polynesia";
Console.WriteLine(url + parm);
Console.WriteLine(url + HttpUtility.UrlEncode(parm), System.Text.Encoding.UTF8);
Console.WriteLine(url + HttpUtility.UrlPathEncode(parm), System.Text.Encoding.UTF8);
Console.WriteLine(HttpUtility.UrlEncode(url + parm), System.Text.Encoding.UTF8);
Run Code Online (Sandbox Code Playgroud)

您将得到以下输出:

https://www.google.co.uk/search?q=Bora Bora, French Polynesia
https://www.google.co.uk/search?q=Bora+Bora%2c+French+Polynesia
https://www.google.co.uk/search?q=Bora%20Bora,%20French%20Polynesia
https%3a%2f%2fwww.google.co.uk%2fsearch%3fq%3dBora+Bora%2c+French+Polynesia
Run Code Online (Sandbox Code Playgroud)

通过将这些内容粘贴到浏览器中并尝试使用它们,您很快就会看到什么是有效的 URL,什么是无效的。

(注意,当粘贴到现代浏览器中时,如果您的参数无效,许多浏览器会自动为您进行 URL 编码 - 所以您会发现第一个输出也有效,但如果您尝试通过一些 C# 代码调用它,它会失败。)

工作演示: https: //dotnetfiddle.net/gqFsdK

您当然可以将输入的值更改为您喜欢的任何值。它们可以是硬编码字符串,也可以是返回字符串的其他代码的结果(例如,从会话、数据库、UI 元素或其他任何地方获取)。

注意:澄清有效URL 只是 URL 正确格式的字符串也很有用。它与实际存在的URL 不同。如果您尝试使用某个 URL,它可能有效但不存在,也可能有效但确实存在。