ASP.NET MVC ビューで現在のアクションを返すにはどうすればよいですか? 質問する

ASP.NET MVC ビューで現在のアクションを返すにはどうすればよいですか? 質問する

現在のコントローラーとアクションに依存する CSS クラスをマスター ページに設定したいと考えていました。 を介して現在のコントローラーにアクセスできますViewContext.Controller.GetType().Nameが、現在のアクション (など) を取得するにはどうすればよいですIndexShow?

ベストアンサー1

RCでは、アクションメソッド名のようなルートデータを次のように抽出することもできます。

ViewContext.Controller.ValueProvider["action"].RawValue
ViewContext.Controller.ValueProvider["controller"].RawValue
ViewContext.Controller.ValueProvider["id"].RawValue

MVC 3 のアップデート

ViewContext.Controller.ValueProvider.GetValue("action").RawValue
ViewContext.Controller.ValueProvider.GetValue("controller").RawValue
ViewContext.Controller.ValueProvider.GetValue("id").RawValue

MVC 4 のアップデート

ViewContext.Controller.RouteData.Values["action"]
ViewContext.Controller.RouteData.Values["controller"]
ViewContext.Controller.RouteData.Values["id"]

MVC 4.5 のアップデート

ViewContext.RouteData.Values["action"]
ViewContext.RouteData.Values["controller"]
ViewContext.RouteData.Values["id"]

おすすめ記事