当前位置: 移动技术网 > IT编程>开发语言>.net > 详解ASP.NET Core 2.0 路由引擎之网址生成(译)

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

2017年11月17日  | 移动技术网IT编程  | 我要评论

花亭湖钓鱼,比比贴信息网,高清影视下载

问题

如何在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属性供我们使用。 

原文:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网