querystring のように読み取る代わりに、ビュー ページでルート値を取得する代替手段はありますか?
@Html.ActionLink("Language Resources", "Index", "LanguageResource",
new { languageCode = Request.QueryString["languageCode"] , "")
querystring のように読み取る代わりに、ビュー ページでルート値を取得する代替手段はありますか?
@Html.ActionLink("Language Resources", "Index", "LanguageResource",
new { languageCode = Request.QueryString["languageCode"] , "")
RequestContext を使用できます
@{
var id = HttpContext.Current.Request.RequestContext.RouteData.Values["id"];
}
シンプルに保つことを除外しないでください。
public ActionResult Action(int id)
{
return View( id);
}
扱っているモデルのタイプを示します
@model int
そして、次の方法で強く型付けされた方法で値を参照します。
@Model
また、経験の浅い方は、コントローラーと URL のアクション部分にもアクセスできます。(カミソリの例)
if(ViewContext.RouteData.Values["action"].ToString() == "Account")
{
@Html.Raw("Hi ") @Session["UserName"]
}
どこ、
context.MapRoute(
name: "MyRoute",
url: "{controller}/{action}/{id}"
}