ASP.NET /Mono MVC4购物车应用程序用户产品代码作为awstats分析的URL名称,如:
http://myshop.com/Store/Details/PRODUCT1
使用默认路由和控制器
public class StoreController : MyApp.ControllerBase
{
public ActionResult Details(string id)
{
....
PRODUCT1是产品代码。 如果产品代码包含目录名中的/和其他不允许的字符,则Web服务器会抛出错误。
产品代码应该是人类可读的,因为分析显示了网址。 如何将产品代码编码为目录名称并在控制器中解码? 编码后,产品代码应保持唯一。
ASP.NET中是否有一些内置函数或一些实现此功能的源代码?
使用jquery,jquery ui,razor视图引擎。
更新
我通过创建Razor助手
在评论中尝试了推荐@helper Details(string toode)
{
@Url.Action("Details", new { id = HttpUtility.UrlEncode(toode) });
}
然后使用
<a href="@Details(someproduct)">
这会导致错误:
从客户端(%)检测到潜在危险的Request.Path值。 GET / Store / Details / S%c3%9c%c3%9cTAJA HTTP / 1.0
url路径中不允许%chareter。 所以UrlEncode无法使用。如何编码/解码?
答案 0 :(得分:0)
您可以使用加密和解密算法(加密)来实现这样的目标......
public class CryptoEngine
{
private static CryptoEngine _instance;
private CryptoEngine() { }
public static CryptoEngine Instance
{
get
{
if (_instance == null)
_instance = new CryptoEngine();
return _instance;
}
}
static readonly string PasswordHash = "@dM!nCo$";
static readonly string SaltKey = "AdMinCos";
static readonly string VIKey = "Polar!s@dM!nCoN$";
public string Encrypt(string plainText)
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
byte[] cipherTextBytes;
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
cipherTextBytes = memoryStream.ToArray();
cryptoStream.Close();
}
memoryStream.Close();
}
return Convert.ToBase64String(cipherTextBytes);
}
public string Decrypt(string encryptedText)
{
encryptedText = encryptedText.Trim();
if (encryptedText.Contains(" "))
{
encryptedText = encryptedText.Replace(" ", "+");
}
byte[] cipherTextBytes = Convert.FromBase64String(encryptedText);
byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };
var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
var memoryStream = new MemoryStream(cipherTextBytes);
var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
}
}
并像这样使用它......
"/Store/Details?id="+CryptoEngine.Instance.Encrypt(toode))
在你的行动中,你可以像这样解密它......
public ActionResult Details(string id)
{
string _id = CryptoEngine.Instance.Decrypt(id);
.......
您可以设置PasswordHash,SaltKey和VIKey 我想这就是你想要的。
您可以查看有关加密的更多信息 Reference