查询字符串发送到MVC控制器得到编码
问题描述:
我是新的论坛,也很新的MVC。 我有一个MVC应用程序,它在我的本地开发环境中测试时运行OK,但是当部署在IIS7上时,我遇到了一些问题,其中之一是在从查询字符串中传递参数的控制器调用操作时出现404错误,“?”和“=”是由IIS编码的,我想这就是它失败的原因。 我的控制器动作:查询字符串发送到MVC控制器得到编码
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();
}
这在一个局部视图<%Html.RenderAction使用( “CreateMenu”, “菜单”); %>在主视图上显示正确,但是当点击一个项目时,结果是404错误。 我在web.config上更改的一件事是设置为: requestValidationMode =“2.0”requestPathInvalidCharacters =“” 因为我收到错误:从客户端(?)检测到潜在危险的Request.Path值,在添加了这些设置后,Request.Path错误消失,但现在我得到了404。 谢谢。
答
你是Url.Encoding查询字符串中的键/值?
HttpUtility.UrlEncode
你能粘贴失败的网址吗?如果您未对其进行编码(例如'/'),可能会导致问题,您可能在查询字符串中包含一些无效字符。
开发环境中的url:http:// localhost:53170/FtpAccess/Group/14?cbg = 19,在IIS服务器中将其变为:http://someserver.int/FtpAccess/Group/14%253fcbg %3d19,IIS正在编码字符“?”和“=”。在使用部署在服务器中的应用程序的web.config后,我仍然没有运气,但404错过了,但出现了其他错误。 – user1058542