public class CustomRouteConstraint : IRouteConstraint { public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection) { throw new NotImplementedException(); } }
在 Controller 上利用 token 占位符所谓的 token 占位符 就是具有一些特定寄义的占位标记,好比说:[action], [area] 和 [controller],别离暗示用你真实的 Controller 和 Action 去替换,下面的代码展示了如何利用这种模式去实现。
[Route("[controller]/[action]")] public class HomeController : Controller { private readonly ILogger<HomeController> _logger; public HomeController(ILogger<HomeController> logger) { _logger = logger; } public IActionResult Index() { return View(); } //Other action methods }
整体来看,基于特性 的 Route 给了你更多的操控权限,机动的 Route Template 设置实现了 Controller 和 Action 的解耦,虽然这里也不是说 基于约定 的Route 欠好,究竟人家是 Global 级此外,真实场景下两者更多的是混着用。
译文链接:https://www.infoworld.com/article/3569369/how-to-use-attribute-routing-in-aspnet-core.html
到此这篇关于如安在ASP.NET Core中利用Route特性的文章就先容到这了,更多相关ASP.NET Core Route特性内容请搜索剧本之家以前的文章或继承欣赏下面的相关文章但愿各人今后多多支持剧本之家!
您大概感乐趣的文章: