【问题标题】:query string sent to MVC controller gets encoded发送到 MVC 控制器的查询字符串被编码
【发布时间】:2011-11-25 21:05:10
【问题描述】:

我是论坛的新手,对 MVC 也很陌生。 我有一个 MVC 应用程序,在我的本地开发环境中测试时运行良好,但是在 IIS7 上部署时我遇到了一些问题,其中之一是从控制器调用操作时收到 404 错误,并在查询字符串中传递参数,这 ”?”和 "=" 由 IIS 编码,我想这就是它调用失败的原因。 我的控制器操作:

public ActionResult CreateMenu()
        {
            MenuModel.MenuItem menuItem;
            MenuModel.MenuItem childItem;

            if (AuthenticationService.IsUserAuthenticated())
            {
                string u_id = AuthenticationService.GetAuthenticatedUserName();
                Role u_role = Repository.GetUserRole(u_id);

                mainMenu.MenuItems.RemoveAt(0);
                if ((u_role.Role_Desc == "Administrator") || (u_role.Role_Desc == "Manager"))
                {
                    int lastMenuNumber = mainMenu.MenuItems.Count;
                    menuItem = new MenuModel.MenuItem(++lastMenuNumber, "Account Settings", "", "");
                    childItem = new MenuModel.MenuItem(++lastMenuNumber, "Log Report", "LogReporting", "ShowLog");
                    menuItem.ChildItems.Add(childItem);

                    childItem = new MenuModel.MenuItem(++lastMenuNumber, "User Permissions", "PermissionsSetting", "ListPermissions");
                    menuItem.ChildItems.Add(childItem);

                    mainMenu.MenuItems.Insert(0, menuItem);
                }
                // list of accessible BGs for selected client

                var selectedClient = Session["client_Id"] != null ?
                                        Repository.GetClientById((short)Session["client_Id"]) :
                                        Repository.GetUserClients(u_id).First();

                int i = 0;
                var bgs = Repository.GetUserClientAccesibleBusinessGroups(u_id, selectedClient.Client_ID);
                if (bgs.Count() > 0)
                {
                    foreach (var bg in bgs)
                    {                                                                                              

                        menuItem = new MenuModel.MenuItem(mainMenu.MenuItems.Count + 1, bg.BG_Desc, "FtpAccess", String.Format("Group/{0}?cbg={1}", selectedClient.Client_ID, bg.Client_BG_ID));
                        mainMenu.MenuItems.Insert(i++, menuItem);
                    }
                }
            }

            ViewData.Model = mainMenu;
            return View();
        }

这在局部视图中使用 在主视图上,它被正确渲染,但是当点击一个项目时,结果是 404 错误。 我在 web.config 上更改的一件事是以下设置: requestValidationMode="2.0" requestPathInvalidCharacters="" 因为我收到错误:从客户端(?)检测到潜在危险的 Request.Path 值,添加这些设置后,Request.Path 错误消失,但现在我得到 404。

任何帮助。 谢谢。

【问题讨论】:

    标签: asp.net-mvc iis-7


    【解决方案1】:

    您是否在 Url.Encoding 查询字符串中的键/值?

    HttpUtility.UrlEncode
    

    你能粘贴失败的网址吗?如果您未对其进行编码(例如“/”),则查询字符串中可能包含一些无效字符,这可能会导致问题。

    【讨论】:

    猜你喜欢
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-25
    • 1970-01-01
    • 2017-11-01
    相关资源
    最近更新 更多