【问题标题】:ASP.NET MVC 4 Routing Query - Passing query-string to Index ActionASP.NET MVC 4 路由查询 - 将查询字符串传递给索引操作
【发布时间】:2012-10-07 22:52:08
【问题描述】:

我有一个带有索引操作的控制器。

public ActionResult Index(int id = 0)
{

    return view();
}

我希望将 id 传递给 index 操作,但它的工作方式似乎与 details 操作不同。

例如如果我想将 id 4 传递给 index 操作,我必须访问 url:

http://localhost:8765/ControllerName/?id=4

有了详细的操作...我可以做到这一点。

http://localhost:8765/ControllerName/Details/4

我想对 Index 做的事情类似于...

http://localhost:8765/ControllerName/4

当我访问这个网址时,我得到一个错误:

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /fix/1

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929

这可能吗?如何让 MVC 以与详细信息相同的方式自动处理索引操作?

谢谢

更新 - 我当前的路线配置

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

当我访问 localhost:1234/Fix/3 时,更新新的 RouteConfig 类仍然不起作用

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

            routes.MapRoute(
            name: "FixIndexWithParam",
            url: "Fix/{id}",
            defaults: new { controller = "Fix", action = "Index", id = UrlParameter.Optional });
    }
}

【问题讨论】:

  • 我们需要查看您的RoutesConfig.cs 文件中的内容
  • 用我的 RouteConfig 更新的问题
  • 仅供参考:我编辑了您的问题以删除对 EntityFramework 的引用,这个问题与 EF 无关,这完全与 .NET 路由实现以及它将这些请求映射到 MVC 控制器的方式有关跨度>

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


【解决方案1】:

更新值得指出的是,/ControllerName/Index/4 应该使用默认路由。

使用默认路由,它期望第二个参数是控制器名称。

因此,默认路由 /ControllerName/4 被解释为 ControllerNameController Action 4,这当然不存在。

如果你添加

routes.MapRoute(
name: "IndexWithParam",
url: "{controller}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });

在它允许的默认值之前

/Home/4 被路由到HomeController action Indexid=4

我没有测试过,它可能与默认值冲突。您可能需要在路由中明确指定控制器,即:

routes.MapRoute(
name: "HomeIndexWithParam",
url: "Home/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });

(显然,将Home 替换为您实际想要路由到的任何控制器)

【讨论】:

  • 谢谢,我创建了一条新路线,但仍有问题...请参阅更新后的问题。
  • 对不起,我在粘贴之前回复了。请检查更新
  • 你需要在默认路由之前添加新路由,它们是按顺序执行的
  • 还可以添加路由约束,这样只有参数为整数时才会匹配。
  • 如何附加路由约束?
猜你喜欢
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 1970-01-01
  • 2016-09-18
相关资源
最近更新 更多