asp.net Core1.1版本生成超链接/a链接标签的方式

1、传统方式

第一种:@Html.ActionLink("Register", "Register", "Account")
第二种:<a href="@Url.Action("Register", "Account")">Register</a>

生成的Html代码

第一种:<a href="/Account/Register">Register</a>
第二种:<a href="/Account/Register">Register</a>

2、asp.net core新增的方式

<a asp-controller="Account" asp-action="Register">Register</a>
注意:asp-controller为指定的控制器,asp-action为控制器中的方法,也就是Action

3、如果要指定参数的话

<a asp-controller="Product" asp-action="Display" asp-route-id="@ViewBag.ProductId">View Details</a>
asp-route-id为传递给Action的参数

生成的html代码

<a href="/Product/Display/1">View Details</a>

4、通过路由名称指定

routes.MapRoute(
    name: "login",
    template: "login",
    defaults: new { controller = "Account", action = "Login" });
比如是上面这个路由

<a asp-route="login">Login</a>

通过asp-route指定就可以了

5、通过http协议,IP或者域名指定

@Html.ActionLink("Register", "Register", "Account",  "https", "aspecificdomain.com", "fragment", null, null)
生成的链接

<a href="https://aspecificdomain.com/Account/Register#fragment">Register</a>

6、如果你要指定网站当前的域名

@Html.ActionLink("Register", "Register", "Account",  "https", null, null, null, null)
7、asp.net core中新增的方法

<a asp-controller="Account" 
   asp-action="Register" 
   asp-protocol="https" 
   asp-host="asepecificdomain.com" 
   asp-fragment="fragment">Register</a>

<!--或者使用下面的方法 -->
<a asp-controller="Account" 
   asp-action="Register" 
   asp-protocol="https">Register</a>


翻译原文(有删改): ASP.NET Core MVC Anchor Tag Helper









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值