我需要有人指出我关于属性路由的正确方向。 我遇到的问题是我想扩展默认的Route属性,以便我可以根据各种条件检查路由是否应该映射或不映射(在 routes.MapMvcAttributeRoutes()期间)。我创建了一个继承自Attribute并实现IDirectRouteFactory和IRouteInfoProvider的类,但我真的不知道如何使MapMvcAttributeRoutes()忽略我的自定义属性。我的第一个想法是返回一个null RouteEntry,但似乎不起作用。有什么想法吗?
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
public class CustomRouteAttribute : Attribute, IDirectRouteFactory, IRouteInfoProvider
{
string _Template;
public CustomRouteAttribute(string template, string controllerName)
{
if (ControllerUtil.IsControllerActive(controllerName))
{
//I do want this route to be mapped
_Template = template;
}
else
{
//I do not want this route to be mapped
}
}
public RouteEntry CreateRoute(DirectRouteFactoryContext context)
{
IDirectRouteBuilder builder = context.CreateBuilder(Template);
builder.Name = Name;
return builder.Build();
}
public string Name
{
get;
set;
}
public string Template
{
get
{
return _Template;
}
}
}