【问题标题】:Mvc4 Querystring parameter is not workingMvc4 Querystring 参数不起作用
【发布时间】:2013-08-12 09:31:41
【问题描述】:

在 Asp.net MVC4 中,我创建了以下 URL,显示 IIS8.0 出现 404 错误

http://{ParentURL}/Areas/Admin/Menu/Index?actoin=Add

请帮帮我。

【问题讨论】:

  • 你使用默认路由吗?您应该在“管理”区域中有一个“菜单”控制器,并且操作/视图应该是“索引”。
  • 问题是只有查询 sring 参数只有其他明智的工作正常但是当我通过 ?mode = 添加然后 404 错误来了?所以我有新路线?

标签: asp.net asp.net-mvc-4 asp.net-mvc-routing


【解决方案1】:

您的 URL http://{ParentURL}/Areas/Admin/Menu/Index?mode=Add 转到:

区域:管理员

控制器:菜单

动作+视图:索引

参数:模式

所以在 MenuController.cs(在管理区域下)你应该有这个:

public ActionResult Index(string mode)
{
   //code
   return View();
}

更新:

将您的网址更改为:http://{ParentURL}/Admin/Menu/Index?mode=Add

Action 的参数必须与路由声明中的名称相同。 另一个错误是在您的 URL 中使用Areas,您可以将其删除或更改您的默认路由。

控制器:

public ActionResult Index(string mode)
{
   return View(ViewMenuModel);
}

查看:

var JsSuccessAction = '@Url.Content("~/Admin/Menu/Index?mode=Add")';

路线:

public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute( "Admin_default", "Admin/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } );
}

【讨论】:

  • 控制器 ====================== public ActionResult Index(string mode) { return View(ViewMenuModel); } 查看 ===================== var JsSuccessAction = '@Url.Content("~/Areas/Admin/Menu/Index?mode=Add")'; public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Admin_default", "Admin/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); }
  • 看看我更新的答案。是的,您不需要区域名称,如果需要,您必须将其添加到您的路线声明中。
  • 是的,Right Areas 正在制造问题。
【解决方案2】:

你需要在路由配置中设置一个路由:

context.MapRoute(
    "Areas",
    "Areas/{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional }
);

将此添加到您的根配置并尝试 .. 将起作用

【讨论】:

  • 我还需要添加Routes.config,它已经添加到Admin Area Routes
  • 你的路由错了,他需要在路由中添加“Admin”,查看我的答案。
猜你喜欢
  • 2021-04-24
  • 1970-01-01
  • 2016-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多