【发布时间】: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