如何模仿ASP Server.URLEn code在ASP.NET? [英] How to mimic ASP Server.URLEncode in ASP.NET?

查看:169
本文介绍了如何模仿ASP Server.URLEn code在ASP.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP

Server.URLEncode("+&(). -*<>/\|")
' returns %2B%26%28%29%2E+%2D%2A%3C%3E%2F%5C%7C

在ASP.NET

Uri.EscapeDataString("+&(). -*<>/\|")
// returns %2B%26().%20-*%3C%3E%2F%5C%7C

HttpUtility.UrlEncode("+&(). -*<>/\|") 
// returns %2b%26().+-*%3c%3e%2f%5c%7c

有没有优雅的方式如何模仿ASP.NET老ASP的行为?

Is there any elegant way how to mimic old ASP behavior in ASP.NET?

推荐答案

您可以使用常规的前pression以匹配要转换的字符,一个lambda前pression创建六角code:

You can use a regular expression to match the characters that you want to convert, and a lambda expression for creating the hex code:

string input = @"+&(). -*<>/\|";
string encoded = Regex.Replace(
  HttpUtility.UrlEncode(input),
  @"[()\.\-*]",
  m => "%" + Convert.ToString((int)m.Captures[0].Value[0], 16)
);

这篇关于如何模仿ASP Server.URLEn code在ASP.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆