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

1、传统方式

 

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

 

生成的Html代码

 

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

 

 

2、asp.net core新增的方式

 

 

  1. <asp-controller="Account" asp-action="Register">Register</a>  

注意:asp-controller为指定的控制器,asp-action为控制器中的方法,也就是Action

 

3、如果要指定参数的话

 

  1. <asp-controller="Product" asp-action="Display" asp-route-id="@ViewBag.ProductId">View Details</a>  

asp-route-id为传递给Action的参数

 

生成的html代码

 

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

 

 

4、通过路由名称指定

 

 

  1. routes.MapRoute(  
  2.     name: "login",  
  3.     template: "login",  
  4.     defaults: new { controller = "Account", action = "Login" });  

比如是上面这个路由

 

 

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

 

通过asp-route指定就可以了

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

 

  1. @Html.ActionLink("Register", "Register", "Account",  "https", "aspecificdomain.com", "fragment", null, null)  

生成的链接

 

 

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

 

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

 

 

  1. @Html.ActionLink("Register", "Register", "Account",  "https", null, null, null, null)  

7、asp.net core中新增的方法

 

 

  1. <asp-controller="Account"   
  2.    asp-action="Register"   
  3.    asp-protocol="https"   
  4.    asp-host="asepecificdomain.com"   
  5.    asp-fragment="fragment">Register</a>  
  6.   
  7. <!--或者使用下面的方法 -->  
  8. <asp-controller="Account"   
  9.    asp-action="Register"   
  10.    asp-protocol="https">Register</a>  



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

posted @ 2017-07-04 15:01  越界  阅读(771)  评论(0编辑  收藏  举报