ASP.NET Core 路由使用下面的帮助标签生成action方法的链接
1 asp-controller
2 asp-action.
应用程序是如何根据定义的路由创建链接的呢?让我们创建一个ASP.NET Core MVC应用程序,命名为AspNetCore.RouteLinks
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
<a asp-controller="Home" asp-action="Check">Outgoing URL</a>
<a href="/Home/Check">Outgoing URL</a>
如下图所示:
<a asp-controller="Customer" asp-action="Check">Check action of Customer Controller</a>
在这种情况下,生成如下链接:
<a href="/Customer/Check">Check action of Customer Controller</a>
<a asp-action="Index">Go to Index</a>
在这种情况下,你会发现在HTML中生成的地址仅仅包含/ 而不是Home/Index,如下所示:
<a href="/">Go to Index</a>
using Microsoft.AspNetCore.Mvc;
namespace RouteLinks.Controllers
{
[Route("News/[controller]/USA/[action]/{id?}")]
public class AdminController : Controller
{
public IActionResult Index()
{
return View();
}
}
}
在AdminController上添加一个路由,使用attribute 路由生成一个链接
<a asp-controller="Admin" asp-action="Index">Index Action of Admin Controller</a>
下面是Attribute 路由生成的链接:
<a href="/News/Admin/USA/Index">Index Action of Admin Controller</a>
4 当出现多个路由时生成外部链接
app.MapControllerRoute(
name: "defaultonly",
pattern: "{controller}/{action}",
defaults: new { controller = "USA" });
app.MapControllerRoute(
name: "stock",
pattern: "Stock/{action}",
defaults: new { controller = "Home" });
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
在Home控制器的Index视图中添加下面代码:
<a asp-action="Index">Link1</a>
<a asp-controller="Product">Link2</a>
<a href="/Stock/Index">Link1</a>
<a href="/Product">Link2</a>
5 asp-route-{value}
到目前为止,您已经看到了如何使用控制器和方法创建链接,如果我们想要给其它段变量传递值,如id?,我们可以使用asp-route-{value}帮助标签,将{value}替换变量段的名称,如id,让我们创建一个链接,并传递一个Id的值
using Microsoft.AspNetCore.Mvc;
namespace RouteLinks.Controllers
{
public class ProductController : Controller
{
public string Index(int id)
{
return "Id Value is: " + id;
}
}
}
<a asp-controller="Product" asp-action="Index" asp-route-id="100">Pass 100 to the id segment</a>
现在运行应用程序并且检查html中生成的链接:
<a href="/Product/Index/100">Pass 100 to the id segment</a>
点击链接,将会跳转到Product控制器的Index方法,我们将显示Id段的值,这个段的值是100,使用asp-route-{value}帮助标签来设置该值
6 根据指定路由生成链接
我们还可以使用路由名字生成链接,这时需要我们给asp-route标签指定一个具体的路由名称,代码如下:
app.MapControllerRoute(
name: "sales",
pattern: "sales/{controller=Home}/{action=Index}");
我们现在使用路由来创建一个链接,使用asp-route帮助标签,asp-route="sales",sales是路由的名称
在View中添加下面代码:
<a asp-route="sales">Sales</a>
将会生成如下地址:
<a href="/sales">Sales</a>
<p>@Url.Action("List", "Product", new { id = 10 })</p>
生成的url
/Product/List/10
也可以在action方法中使用Url.Action() ,代码如下:
string url = Url.Action("Index", "Home", new { id = 100 });
这里第一个参数是Action名称,第二个参数是Controller名称,第三个参数是asp-route-{value}
8 URL 片段(#)
<a href="/Product/List#Printing">Printing</a>
<a asp-controller="Product" asp-action="List" asp-fragment="Printing">
URL Fragment
</a>
RouteOption可以设置URL生成以至于在URL末尾能包含斜杠(/),进入Program.cs类并且添加代码:
builder.Services.Configure<RouteOptions>(options =>
{
options.AppendTrailingSlash = true;
});
将会生成如下url
https://localhost:7248/Stock/Index/
https://localhost:7248/Product/
https://localhost:7248/Product/Index/100/
10 小写URL
builder.Services.Configure<RouteOptions>(options =>
{
options.LowercaseUrls = true;
});
路由中间件会将url设置为小写形式
例如:check 方法的url http://localhost:58470/home/check/
源代码地址
https://github.com/bingbing-gui/Asp.Net-Core-Skill/tree/master/Fundamentals/AspNetCore.Route/AspNetCore.RouteLinks