Url.Action()和Html.ActionLink()的用法,主要功能是生成一个超链接。无论路由配置中的“/”符号换成什么符号都可以正确的生成一个超链接,并确保能访问。这就是这两个方法的主要作用。
一、路由配置文件:
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace qyglweb
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "index", id = UrlParameter.Optional }
);
}
}
}
二、HtmlHelper的方法
<a demo="sss" href="<%:Url.Action("index","UserInfo") %>">链接到用户列表</a>
<hr />
<%:
Html.ActionLink("这是一个超链接!","index","UserInfo",new {name="itcast"},new {demo="sss"})
%>
<%: Html.TextBox("UserName","你好世界!")%>
<%: Html.TextArea("UserName","你好世界!",)%>
生成的链接:/UserInfo/index?name=itcast?demo=sss