[教程]ASP.Net MVC的Html.ActionLink解析

Html.ActionLink 方法用于指向Controller的Action
ActionLink方法定义的代码

//页面显示linkText指向Global.asax.cs中默认的Controller对象的actionName方法
public string ActionLink(string linkText, string actionName) {
if (String.IsNullOrEmpty(linkText)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
}
if (String.IsNullOrEmpty(actionName)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
}
return GenerateLink(linkText, null /* routeName */, actionName, null /* controllerName */, new RouteValueDictionary());
}
//页面显示linkText指向默认Global.asax.cs中默认的Controller对象的actionName方法并且带的参数值values
public string ActionLink(string linkText, string actionName, object values) {
if (String.IsNullOrEmpty(linkText)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
}
if (String.IsNullOrEmpty(actionName)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
}
return GenerateLink(linkText, null /* routeName */, actionName, null /* controllerName */, new RouteValueDictionary(values));
}
//页面显示linkText指向定义的RouteValueDictionary中的Controller对象的actionName方法
public string ActionLink(string linkText, string actionName, RouteValueDictionary valuesDictionary) {
if (String.IsNullOrEmpty(linkText)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
}
if (String.IsNullOrEmpty(actionName)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
}
if (valuesDictionary == null) {
throw new ArgumentNullException("valuesDictionary");
}
return GenerateLink(linkText, null /* routeName */, actionName, null /* controllerName */, new RouteValueDictionary(valuesDictionary));
}
//页面显示linkText指向ControllerName对应的Controller的actionName方法
public string ActionLink(string linkText, string actionName, string controllerName) {
if (String.IsNullOrEmpty(linkText)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
}
if (String.IsNullOrEmpty(actionName)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
}
if (String.IsNullOrEmpty(controllerName)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "controllerName");
}
return GenerateLink(linkText, null /* routeName */, actionName, controllerName, new RouteValueDictionary());
}
//
public string ActionLink(string linkText, string actionName, string controllerName, object values) {
if (String.IsNullOrEmpty(linkText)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
}
if (String.IsNullOrEmpty(actionName)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
}
if (String.IsNullOrEmpty(controllerName)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "controllerName");
}
return GenerateLink(linkText, null /* routeName */, actionName, controllerName, new RouteValueDictionary(values));
}
public string ActionLink(string linkText, string actionName, string controllerName, RouteValueDictionary valuesDictionary) {
if (String.IsNullOrEmpty(linkText)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
}
if (String.IsNullOrEmpty(actionName)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
}
if (String.IsNullOrEmpty(controllerName)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "controllerName");
}
if (valuesDictionary == null) {
throw new ArgumentNullException("valuesDictionary");
}
return GenerateLink(linkText, null /* routeName */, actionName, controllerName, new RouteValueDictionary(valuesDictionary));
}

使用实例代码

<li>
<%= Html.ActionLink("Home","Index")%>
</li>
<li>
<%= Html.ActionLink("About","About")%>
</li>
<li>
<%=Html.ActionLink("Products","Detail","Products" )%>
</li>
<li>
<%=Html.ActionLink("Products","Detail","Products" ,2)%>
</li>
赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏