是否有替代方法可以在视图页面中获取路由值,而不是像 querystring 那样读取它?
@Html.ActionLink("Language Resources", "Index", "LanguageResource",
new { languageCode = Request.QueryString["languageCode"] , "")
是否有替代方法可以在视图页面中获取路由值,而不是像 querystring 那样读取它?
@Html.ActionLink("Language Resources", "Index", "LanguageResource",
new { languageCode = Request.QueryString["languageCode"] , "")
尝试从下面的代码中找到
在剃刀
@{
var id = ViewContext.RouteData.Values["id"];
}
在 Web 表单中:
<%
var id = ViewContext.RouteData.Values["id"];
%>
您可以使用 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}"
}