是否有替代方法在视图页中获取路由值,而不是像查询字符串那样读取它?如何在视图中获取路由值(Asp.net Mvc)
@Html.ActionLink("Language Resources", "Index", "LanguageResource",
new { languageCode = Request.QueryString["languageCode"] , "")
是否有替代方法在视图页中获取路由值,而不是像查询字符串那样读取它?如何在视图中获取路由值(Asp.net Mvc)
@Html.ActionLink("Language Resources", "Index", "LanguageResource",
new { languageCode = Request.QueryString["languageCode"] , "")
不排除保持简单:
public ActionResult Action(int id)
{
return View(id);
}
指出,我们正在处理
@model int
,并通过参考值的强类型的方式模型的类型:
@Model
您可以使用RequestContext的
@{
var id = HttpContext.Current.Request.RequestContext.RouteData.Values["id"];
}
尝试使用下面的代码来找到
在剃刀
@{
var id = ViewContext.RouteData.Values["id"];
}
在Web窗体:
<%
var id = ViewContext.RouteData.Values["id"];
%>
对于那些经验较少的人,您可以访问URL的控制器和动作部分。 (剃刀实施例)
if(ViewContext.RouteData.Values["action"].ToString() == "Account")
{
@Html.Raw("Hi ") @Session["UserName"]
}
凡
context.MapRoute(
name: "MyRoute",
url: "{controller}/{action}/{id}"
}
返回对象,如何转换为DIFF类型? –
@Nigel'int id = ViewContext.RouteData.Values [“id”] as int;' – Sinjai
糟糕,我们正在处理一个ValueType。 '诠释? id = ViewContext.RouteData.Values [“id”] as int?;'将其转换为[可空数字](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/nullable-types /)AKA'int?',然后检查它是否为空。 –
Sinjai