[译]ASP.NET Core 2.0 路由引擎之网址生成
问题
如何在ASP.NET Core 2.0中由路由引擎来生成网址?
答案
新建一个空项目,修改Startup.cs文件,添加MVC服务和中间件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseMvc(routes => { routes.MapRoute( name: "goto_one" , template: "one" , defaults: new { controller = "Home" , action = "PageOne" }); routes.MapRoute( name: "goto_two" , template: "two/{id?}" , defaults: new { controller = "Home" , action = "PageTwo" }); routes.MapRoute( name: "default" , template: "{controller=Home}/{action=Index}/{id?}" ); }); } |
添加一个MobileController控制器类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | public class MobileController : Controller { public IActionResult Index() { var url = Url.Action( "Index" ); // /mobile return Content($ "Mobile/Index (Url: {url})" ); } public IActionResult PageOne() { var url = Url.Action( "PageOne" ); // /mobile/PageOne return Content($ "Mobile/One (Url: {url})" ); } [HttpGet] public IActionResult PageTwo() { var url = Url.Action( "PageTwo" ); // /mobile/PageTwo OR /mobile/PageTwo/1? return Content($ "(GET) Mobile/Two (Url: {url})" ); } [HttpPost] public IActionResult PageTwo( int id) { var url = Url.Action( "PageTwo" ); // /mobile/PageTwo/1 return Content($ "(POST) Mobile/Two: {id} (Url: {url})" ); } public IActionResult PageThree() { var url = Url.RouteUrl( "goto_two" , new { id = 5 }); // /two/5 return Content($ "Mobile/Three (Url: {url})" ); } public IActionResult PageFour() { var url = Url.RouteUrl( "goto_two" , new { q = 5 }); // /two?q=5 return Content($ "Mobile/Four (Url: {url})" ); } public IActionResult PageFive() { return RedirectToAction( "PageSix" ); } public IActionResult PageSix() { return Content( "Mobile/Six (Mobile/Five will also come here)" ); } } |
讨论
我们可以使用MVC的路由机制来生成网址,而无需在应用程序中硬编码网址。MVC有这么做的所有信息,来自于我们设置路由映射所提供的模板。
MVC提供了IUrlHelper接口来提供生成网址的功能。这是通过在控制器基类,视图和试图组件公开Url属性来实现的。
IUrlHelper接口提供两个关键的方法来生成网址:
- Action:通过提供控制器,方法和路由参数值来生成网址。
- RouteUrl: 通过提供路由映射名称和路由参数来生成网址。
如果调用上述方法时未提供控制器和路由参数,那么MVC会从当前请求或者方法参数中获取(即是从当前上下文的环境变量中获取)。下面的方法存在于MobileController控制器中:
1 2 3 4 5 | public IActionResult PageTwo( int id) { var url = Url.Action( "PageTwo" ); // /mobile/PageTwo/1 return Content($ "(POST) Mobile/Two: {id} (Url: {url})" ); } |
路由参数可以作为匿名对象来提供:
1 2 3 4 5 | public IActionResult PageThree() { var url = Url.RouteUrl( "goto_two" , new { id = 5 }); // /two/5 return Content($ "Mobile/Three (Url: {url})" ); } |
如果MVC无法将这些值映射到地址标记,那么这些参数会作为网址的查询字符串拼接起来:
1 2 3 4 5 | public IActionResult PageFour() { var url = Url.RouteUrl( "goto_two" , new { id=5, key1 = "value1" }); // /two/5?key1=value1 return Content($ "Mobile/Four (Url: {url})" ); } |
ControlBase类上有一个很方便的方法RedirectToAction,用来将用户请求重定向到某个控制器方法中,这一过程是在客户端完成的:
1 2 3 4 5 6 7 8 9 | public IActionResult PageFive() { return RedirectToAction( "PageSix" ); } public IActionResult PageSix() { return Content( "Mobile/Six (Mobile/Five will also come here)" ); } |
为了将IUrlHeper作为依赖项注入需要的类中,我们需要首先在ConfigureServices中配置相应的服务:
1 2 3 4 5 6 7 8 9 10 11 | public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IActionContextAccessor, ActionContextAccessor>(); services.AddScoped<IUrlHelper>(factory => { var actionContext = factory.GetService<IActionContextAccessor>().ActionContext; return new UrlHelper(actionContext); }); services.AddMvc(); } |
注:大部分情况下我们无需通过注入来使用IUrlHelper,因为控制器,视图中都已经公开了Url属性供我们使用。
源代码下载
原文:https://tahirnaushad.com/2017/08/20/asp-net-core-mvc-routing/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
2012-11-02 [原创]ExtAspNet秘密花园(二十) — 表格之模板列与编辑框