インデックス アクションを持つコントローラーがあります。
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
私がインデックスでやりたいことは、次のようなものです...
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 でインデックス アクションを詳細アクションと同じように自動的に処理するにはどうすればよいですか?
ありがとう
更新 - 現在のルート構成
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 }
);
}
}
UPDATE NEW 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 });
}
}