services.Configure<MvcOptions>(opt =>
{
opt.EnableTypedRouting();
opt.GetRoute("homepage", c => c.Action<ProductsController>(x => x.Index()));
opt.GetRoute("aboutpage/{name}", c => c.Action<ProductsController>(x => x.About(Param<string>.Any)));
opt.PostRoute("sendcontact", c => c.Action<ProductsController>(x => x.Contact()));
});
public interface IApplicationModelConvention
{
void Apply(ApplicationModel application);
}
public class ApplicationModel
{
public ApplicationModel();
public IList<ControllerModel> Controllers { get; }
public IList<IFilter> Filters { get; }
}
services.Configure<MvcOptions>(opt =>
{
opts.ApplicationModelConventions.Add(new MyApplicationModelConvention());
});
public class TypedRouteModel : AttributeRouteModel
{
public TypedRouteModel(string template)
{
Template = template;
HttpMethods = new string[0];
}
public TypeInfo ControllerType { get; private set; }
public MethodInfo ActionMember { get; private set; }
public IEnumerable<string> HttpMethods { get; private set; }
public TypedRouteModel Controller<TController>()
{
ControllerType = typeof(TController).GetTypeInfo();
return this;
}
public TypedRouteModel Action<T, U>(Expression<Func<T, U>> expression)
{
ActionMember = GetMethodInfoInternal(expression);
ControllerType = ActionMember.DeclaringType.GetTypeInfo();
return this;
}
public TypedRouteModel Action<T>(Expression<Action<T>> expression)
{
ActionMember = GetMethodInfoInternal(expression);
ControllerType = ActionMember.DeclaringType.GetTypeInfo();
return this;
}
private static MethodInfo GetMethodInfoInternal(dynamic expression)
{
var method = expression.Body as MethodCallExpression;
if (method != null)
return method.Method;
throw new ArgumentException("Expression is incorrect!");
}
public TypedRouteModel WithName(string name)
{
Name = name;
return this;
}
public TypedRouteModel ForHttpMethods(params string[] methods)
{
HttpMethods = methods;
return this;
}
}
public class TypedRoutingApplicationModelConvention : IApplicationModelConvention
{
internal static readonly Dictionary<TypeInfo, List<TypedRouteModel>> Routes = new Dictionary<TypeInfo, List<TypedRouteModel>>();
public void Apply(ApplicationModel application)
{
foreach (var controller in application.Controllers)
{
if (Routes.ContainsKey(controller.ControllerType))
{
var typedRoutes = Routes[controller.ControllerType];
foreach (var route in typedRoutes)
{
var action = controller.Actions.FirstOrDefault(x => x.ActionMethod == route.ActionMember);
if (action != null)
{
action.AttributeRouteModel = route;
//注意这里是直接替换,会影响现有Controller上的Route特性定义的路由
foreach (var method in route.HttpMethods)
{
action.HttpMethods.Add(method);
}
}
}
}
}
}
}
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());
}
}
public static class Param<TValue>
{
public static TValue Any
{
get { return default(TValue); }
}
}
opt.GetRoute("aboutpage/{name}", c => c.Action<HomeController>(x => x.About(Param<string>.Any)));
opt.GetRoute("homepage", c => c.Action<HomeController>(x => x.Index())).WithName("foo");
public class ProductsController : Controller
{
[Route("index")]
public IActionResult Index()
{
return Content("Index");
}
}
opt.GetRoute("homepage", c => c.Action<ProductsController>(x => x.Index()));
[Route("products")]
public class ProductsController : Controller
{
public IActionResult Index()
{
return Content("Index");
}
}
opt.GetRoute("/homepage", c => c.Action<ProductsController>(x => x.Index()));
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有