解读ASP.NET 5 & MVC6系列教程(12):基于Lamda表达式的强类型R
| 在该类中,保存了一个静态变量Routes,用于保存所有以Lamda表达式方式声明的路由,然后在现有的Controllers集合中进行查找及修改,然后替换 在这里,我们只是简单替换 优化的时候,要注意Controller上的 然后,在MvcOptions上,我们再为TypeRouteModel添加一些扩展方法以方便使用,代码如下: 
public static class MvcOptionsExtensions
{
 public static TypedRouteModel GetRoute(this MvcOptions opts, string template, Action<TypedRouteModel> configSetup)
 {
  return AddRoute(template, configSetup).ForHttpMethods("GET");
 }
 public static TypedRouteModel PostRoute(this MvcOptions opts, string template, Action<TypedRouteModel> configSetup)
 {
  return AddRoute(template, configSetup).ForHttpMethods("POST");
 }
 public static TypedRouteModel PutRoute(this MvcOptions opts, string template, Action<TypedRouteModel> configSetup)
 {
  return AddRoute(template, configSetup).ForHttpMethods("PUT");
 }
 public static TypedRouteModel DeleteRoute(this MvcOptions opts, string template, Action<TypedRouteModel> configSetup)
 {
  return AddRoute(template, configSetup).ForHttpMethods("DELETE");
 }
 public static TypedRouteModel TypedRoute(this MvcOptions opts, string template, Action<TypedRouteModel> configSetup)
 {
  return AddRoute(template, configSetup);
 }
 private static TypedRouteModel AddRoute(string template, Action<TypedRouteModel> configSetup)
 {
  var route = new TypedRouteModel(template);
  configSetup(route);
  if (TypedRoutingApplicationModelConvention.Routes.ContainsKey(route.ControllerType))
  {
   var controllerActions = TypedRoutingApplicationModelConvention.Routes[route.ControllerType];
   controllerActions.Add(route);
  }
  else
  {
   var controllerActions = new List<TypedRouteModel> { route };
   TypedRoutingApplicationModelConvention.Routes.Add(route.ControllerType, controllerActions);
  }
  return route;
 }
 public static void EnableTypedRouting(this MvcOptions opts)
 {
  opts.ApplicationModelConventions.Add(new TypedRoutingApplicationModelConvention());
 }
}在上述代码中,我们添加了一个 其它的扩展方法则都是用于声明相关的route,大家注意,在最开头的示例中,我们看到获取action信息的方法是通过委托调用该action方法(但没有真正调用),但是有的方法有参数,那怎么办呢?为此,我们定于一个忽略参数的Param类,代码如下: 
public static class Param<TValue>
{
 public static TValue Any
 {
  get { return default(TValue); }
 }
}这样,我们为含有参数的About方法定于路由的时候,就可以这样来定义了,代码如下: 
opt.GetRoute("aboutpage/{name}", c => c.Action<HomeController>(x => x.About(Param<string>.Any)));另外,由于TypeRouteModel里很多方法都是可以链式调用,所以我们也可以通过这种方式为route指定一个名称,示例代码如下: 
opt.GetRoute("homepage", c => c.Action<HomeController>(x => x.Index())).WithName("foo");至此,整个强类型路由的功能就实现完毕了,大家在使用的时候,就多了一种选择了。 弊端(或Bug) 我们看到,在上面实现 
public class ProductsController : Controller
{
 [Route("index")]
 public IActionResult Index()
 {
  return Content("Index");
 }
}然后又通过Lamda表达式又定义了强类型路由,代码如下: 
opt.GetRoute("homepage", c => c.Action<ProductsController>(x => x.Index()));那么,你只能通过 但是,上述Lamda表达式方式并没有覆盖Controller上定义的Route特性定义,所以如果你在ProductsController上定义了Route特性的话,两者就会组合在一起,例如: 
[Route("products")]
public class ProductsController : Controller
{ 
 public IActionResult Index()
 {
  return Content("Index");
 }
}那么你的访问网址应该是 
opt.GetRoute("/homepage", c => c.Action<ProductsController>(x => x.Index()));那你的访问网址就应该是 参考:http://www.strathweb.com/2015/03/strongly-typed-routing-asp-net-mvc-6-iapplicationmodelconvention/ (编辑:南平站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 

