详解ASP.NET Core 2.0 路由引擎之网址生成(译)

所属分类: 网络编程 / ASP.NET 阅读数: 1821
收藏 0 赞 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接口提供两个关键的方法来生成网址:

1.Action:通过提供控制器,方法和路由参数值来生成网址。
2.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网站首页根据IP自动跳转指定页面的示例

本文介绍的程序主要实现根据IP地址或地址段或IP所在城市进行自动跳转到指定页面的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

asp.net无法获取iis目录的问题解决方法

本文介绍了Asp.Net无法获取IIS拾取目录的解决办法,需要的朋友可以参考下
收藏 0 赞 0 分享

.net中 发送邮件内容嵌入图片的具体实例

这篇文章主要介绍了.net中 发送邮件内容嵌入图片的具体实例,需要的朋友可以参考下
收藏 0 赞 0 分享

在.net中用CheckBoxList实现单选

用CheckBoxList实现单选的原因是我觉得CheckBoxList控件页面展示效果要好看一些,需要的朋友可以参考下
收藏 0 赞 0 分享

ASP.NET―001:GridView绑定List、页面返回值具体实现

这篇文章主要介绍了ASP.NET―GridView绑定List、页面返回值具体实现,需要的朋友可以参考下
收藏 0 赞 0 分享

Ajax实现异步刷新验证用户名是否已存在的具体方法

由于要做一个注册页面,看到许多网站上都是使用Ajax异步刷新验证用户名是否可用的,所以自己也动手做一个小实例
收藏 0 赞 0 分享

ASP.NET汉字转拼音 - 输入汉字获取其拼音的具体实现

这篇文章主要介绍了ASP.NET汉字转拼音 - 输入汉字获取其拼音的具体实现,需要的朋友可以参考下
收藏 0 赞 0 分享

.Net消息队列的使用方法

这篇文章主要介绍了.Net消息队列的使用方法,需要的朋友可以参考下
收藏 0 赞 0 分享

C# web api返回类型设置为json的两种方法

web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢,下面为大家介绍几种不错的方法
收藏 0 赞 0 分享

asp.net获取网站目录物理路径示例

这篇文章主要介绍了asp.net获取网站目录物理路径的方法,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多