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

来源:互联网 发布:中国js 12.7mm狙击步枪 编辑:程序博客网 时间:2024/06/01 10:48

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









0 0
原创粉丝点击