【问题标题】:MVC5 Entity pk string, index and create OK but error 404 in edit, delete and detailsMVC5 Entity pk 字符串、索引和创建正常,但在编辑、删除和详细信息中出现错误 404
【发布时间】:2020-04-15 02:41:11
【问题描述】:

我在名为REGISTROS 的表中有a string pk in oracle。我有一个 ASP.NET MVC 项目并使用实体框架。

我用脚手架构建了一个RegistrosController。我可以列出和创建,但我不能编辑、详细信息或删除。请你帮帮我好吗? id-registros 变量:

接收变量的动作方法:

路由配置文件:

【问题讨论】:

  • 请张贴并正确格式化您的代码作为文本 - 不要只向我们展示您的代码的屏幕截图,这非常烦人,而且在 SO ....
  • 对不起,marc_s。这是我的第一篇文章。

标签: c# asp.net-mvc entity-framework http-status-code-404


【解决方案1】:

很简单。

  1. 因为您的链接网址看起来像/Registros/Edit/3-14/12/19

    MVC URL 使用/ 作为路由(区域、控制器、动作)。

    如果 ID_REGISTRO 是字符串类型号

    为什么不使用-拆分字符串?

    考虑你的pk的编码规则可能为时已晚。

  2. 我的建议是 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

  1. 您可能遇到 IIS 404.11 错误。转到 IIS Admin > MIME > Check Double escape On。
  2. 顺便说一句!带有 url 的 AES Dncrypt 可能会出错。因为+space必须被替换。你可以检查这个答案https://stackoverflow.com/a/10025926/7016640

6 天前。希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多