asp.net-mvc - ASP.NET MVC 4 路由查询 - 将查询字符串传递给索引操作

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

我有一个带有索引操作的 Controller 。

public ActionResult Index(int id = 0)
{

    return view();
}

我希望将 id 传递到索引操作中,但它的工作方式似乎与详细信息操作不同。

例如如果我想将 id 4 传递到索引操作中,我必须访问 url:

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

通过详细操作...我可以做到这一点。

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

我想要对索引做的事情是......

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 });
    }
}

最佳答案

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

使用默认路由时,它期望第二个参数是 Controller 名称。

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

如果添加

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

在默认值之前允许

/Home/4 将路由到带有 id=4HomeController 操作 Index

我没有测试过这个,它可能与默认值冲突。您可能需要在路由中显式指定 Controller ,即:

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

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

关于asp.net-mvc - ASP.NET MVC 4 路由查询 - 将查询字符串传递给索引操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12773547/

相关文章:

asp.net-mvc - 操作方法参数不为空,尽管它应该是

asp.net - Task.Factory.StartNew 对 ASP.Net MVC 有帮助还是有害?

asp.net-mvc - asp.NET : Unknown length MVC paths

asp.net-mvc-routing - ASP.NET Core 1.0 中的属性路由

asp.net-mvc - MVC基于角色的路由

c# - 使用像 ASP.NET MVC 路由这样的路由解析对象

asp.net-mvc - ASP.NET MVC 3 登陆页面

jquery - Ajax 授权失败

asp.net-mvc - 在 IIS 6 上将 AJAX 与 ASP.NET MVC 1.0 结合使用

c# - 替换 HTML 中特定 HTML 子字符串的简单方法?