首页 > 解决方案 > MVC5 Entity pk 字符串、索引和创建正常,但在编辑、删除和详细信息中出现错误 404

问题描述

我在 oracle中有一个字符串 pk 在REGISTROS名为. 我有一个 ASP.NET MVC 项目并使用实体框架。

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

变量

接收变量的动作方法:

动作方法

路由配置文件:

路由配置

标签: c#asp.net-mvcentity-frameworkhttp-status-code-404

解决方案


这很简单。

  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 > 检查双转义打开。
  2. 顺便一提 !带有 url 的 AES Dncrypt 可能会出错。因为+必须space更换。你可以检查这个答案https://stackoverflow.com/a/10025926/7016640

6天前。希望这有帮助!


推荐阅读