c# - MVC5 Entity pk 字符串、索引和创建正常,但在编辑、删除和详细信息中出现错误 404
问题描述
我在 oracle中有一个字符串 pk 在REGISTROS
名为. 我有一个 ASP.NET MVC 项目并使用实体框架。
RegistrosController
我用脚手架搭建了一个。我可以列出和创建,但我不能编辑、详细信息或删除。请你帮帮我好吗?id-registros 的变量:
接收变量的动作方法:
路由配置文件:
解决方案
这很简单。
因为您的链接网址看起来像
/Registros/Edit/3-14/12/19
MVC URL
/
用作路由(区域、控制器、操作)。如果 ID_REGISTRO 是字符串类型号
为什么不使用
-
拆分字符串?也许现在考虑你的pk的编码规则为时已晚。
我的建议是 AES 加密/解密您的 pk 字符串。
@Html.ActionLink("Editor" , "Edit" , new { id = EncryptHelper.EncryptString( "3-14/12/19") })
这是 AES 加密/解密助手
public class EncryptHelper
{
private static string KeyVal = "put your random string here";
private static string IvVal = "put your random string here";
public static string EncryptString(string text)
{
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
aes.Key = Encoding.ASCII.GetBytes(KeyVal);
aes.IV = Encoding.ASCII.GetBytes(IvVal);
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptString(text, aes.Key, aes.IV);
return WebUtility.UrlEncode(Convert.ToBase64String(encrypted));
}
}
public static string DecryptString(string base64Str)
{
base64Str = WebUtility.UrlDecode(base64Str);
base64Str = base64Str.Replace(" ", "+");
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
aes.Key = Encoding.ASCII.GetBytes(KeyVal);
aes.IV = Encoding.ASCII.GetBytes(IvVal);
return DecryptString(
Convert.FromBase64String(base64Str), aes.Key, aes.IV);
}
}
static byte[] EncryptString(string plainText, byte[] Key, byte[] IV)
{
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
return encrypted;
}
static string DecryptString(byte[] cipherText, byte[] Key, byte[] IV)
{
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
string plaintext = null;
using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
}
3.加密后的字符串将是
/Registros/Edit/ZF%252F4Q5oa2zXyzUtDSH33sA%253D%253D
- 您可能会遇到 IIS 404.11 错误。转到 IIS Admin > MIME > 检查双转义打开。
- 顺便一提 !带有 url 的 AES Dncrypt 可能会出错。因为
+
必须space
更换。你可以检查这个答案https://stackoverflow.com/a/10025926/7016640
6天前。希望这有帮助!
推荐阅读
- css - Angular:单元格的 innerHtml 中的 CSS 类使用
- python - Pyspark 按键写入多个输出,无需分区
- json - 如何在笑话测试中模拟 json.parse()
- sql - 处理异常oracle创建表
- java - 用java获取webp的宽度和高度
- hyperlink - 我们可以在投影仪演示文稿中提供指向圣人工作表的链接吗
- java - Java SOCKS 5 代理身份验证
- python - 确保用户在 Python 中指定两个函数参数中的一个且仅一个的最佳方法
- html - 使画布在画布之外可见
- ios - 是否可以使用 carthage 分发二进制 ios 框架