[译]ASP.NET Core 2.0 路由引擎之网址生成
问题
如何在ASP.NET Core 2.0中由路由引擎来生成网址?
答案
新建一个空项目,修改Startup.cs文件,添加MVC服务和中间件:
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控制器类:
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控制器中:
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})");
}
如果MVC无法将这些值映射到地址标记,那么这些参数会作为网址的查询字符串拼接起来:
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,用来将用户请求重定向到某个控制器方法中,这一过程是在客户端完成的:
public IActionResult PageFive()
{
return RedirectToAction("PageSix");
} public IActionResult PageSix()
{
return Content("Mobile/Six (Mobile/Five will also come here)");
}
为了将IUrlHeper作为依赖项注入需要的类中,我们需要首先在ConfigureServices中配置相应的服务:
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/
[译]ASP.NET Core 2.0 路由引擎之网址生成的更多相关文章
- [译]ASP.NET Core 2.0 路由引擎
问题 ASP.NET Core 2.0的路由引擎是如何工作的? 答案 创建一个空项目,为Startup类添加MVC服务和请求中间件: public void ConfigureServices(ISe ...
- [译]ASP.NET Core 2.0 视图引擎
问题 如何在ASP.NET Core 2.0中使用Razor引擎来创建视图? 答案 新建一个空项目,修改Startup.cs,添加MVC服务和请求中间件: public void ConfigureS ...
- [译]ASP.NET Core 2.0 系列文章目录
基础篇 [译]ASP.NET Core 2.0 中间件 [译]ASP.NET Core 2.0 带初始参数的中间件 [译]ASP.NET Core 2.0 依赖注入 [译]ASP.NET Core 2 ...
- [译]ASP.NET Core 2.0 部分视图
问题 如何在ASP.NET Core 2.0中使用部分视图来重用页面的公共部分? 答案 新建一个空项目,在Startup中添加MVC服务和中间件: public void ConfigureServi ...
- [译]ASP.NET Core 2.0 区域
问题 如何将一个规模庞大的ASP.NET Core 2.0应用程序进行逻辑分组? 答案 新建一个ASP.NET Core 2.0空项目,修改Startup类,增加Mvc服务和中间件: public v ...
- [译]ASP.NET Core 2.0 中间件
问题 如何创建一个最简单的ASP.NET Core中间件? 答案 使用VS创建一个ASP.NET Core 2.0的空项目,注意Startup.cs中的Configure()方法: public vo ...
- [译]ASP.NET Core 2.0 带初始参数的中间件
问题 如何在ASP.NET Core 2.0向中间件传入初始参数? 答案 在一个空项目中,创建一个POCO(Plain Old CLR Object)来保存中间件所需的参数: public class ...
- [译]ASP.NET Core 2.0 全局配置项
问题 如何在 ASP.NET Core 2.0 应用程序中读取全局配置项? 答案 首先新建一个空项目,并添加两个配置文件: 1. appsettings.json { "Section1&q ...
- [译]ASP.NET Core 2.0 机密配置项
问题 如何在ASP.NET Core 2.0中保存机密配置项(不用将其暴露给源代码管理器)? 答案 创建一个ASP.NET Core 2.0空项目,在项目节点上点击右键,并点击菜单项 - 管理用户机密 ...
随机推荐
- GCD之死锁体会
1.先看下几句代码 1 2 3 4 5 6 7 dispatch_queue_t serialqueue=dispatch_queue_create("serialqueue", ...
- AngularJS指南文档
点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 核心概念 模板 在Angular应用当中,我们的工作就是将服务器的数据填充到客户端页面模 ...
- AngularJS–service(服务)
点击查看AngularJS系列目录 转载请注明出处:http://www.cnblogs.com/leosx/ 服务 Angular的服务也是使用依赖注入(dependency injection ( ...
- 好用的前端页面性能检测工具—sitespeed.io
引言 最近在做HTTP2技术相关调研,想确认一下HTTP2在什么情境下性能会比HTTP1.x有显著提升,当我把http2的本地环境(nginx+PHP)部署完成后进行相关测试时,我遇到了以下问题: ( ...
- 使用docker部署standalone cinder
| 版权:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接.如有问题,可以邮件:wangxu198709@gmail.com 背景 OpenSta ...
- GCD SUM 强大的数论,容斥定理
GCD SUM Time Limit: 8000/4000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatu ...
- Chinese Rings hdu 2842 矩阵快速幂
Chinese Rings Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- JS在可编辑的div中的光标位置插入内容或表情
<input type="button" value="插入字符" onclick="document.getElementById('test ...
- springboot高并发redis细粒度加锁(key粒度加锁)
本文探讨在web开发中如何解决并发访问带来的数据同步问题. 1.需求: 通过REST接口请求并发访问redis,例如:将key=fusor:${order_id} 中的值+1: 2.场景: 设想,多线 ...
- Database 2 Day DBA guide_Chapter2
website:http://www.oracle.com/webfolder/technetwork/tutorials/obe/db/11g/r2/2day_dba/install/install ...