我有一個索引操作的控制器。ASP.NET MVC 4路由查詢 - 將查詢字符串傳遞給索引操作
public ActionResult Index(int id = 0)
{
return view();
}
我想ID傳遞到索引行動,但它不出現在相同的方式行動的細節工作。
例如如果我想ID 4通入索引的動作,我要拜訪網址:
http://localhost:8765/ControllerName/?id=4
隨着細節動作......我能做到這一點。
http://localhost:8765/ControllerName/Details/4
我想與指數做的是一樣的東西......
http://localhost:8765/ControllerName/4
當我訪問這個URL,我得到一個錯誤:
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以與細節相同的方式自動處理索引操作?
感謝
更新 - 我現在的ROUTES CONFIG
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 }
);
}
}
更新。新RouteConfig類仍然當我訪問本地主機不工作:1234 /修正/ 3
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'文件 – Mark
使用我的RouteConfig更新的問題 – Gravy
僅供參考:我編輯了您的問題以刪除對EntityFramework的引用,這個問題與EF無關,只是關於它。NET路由實現和它將這些請求映射到MVC控制器的方式 –