除了`system.web.mvc.routeattribute`以确保框架找到并注册路由以外的属性



我正在尝试使用MVC 3.0.0.1迁移到最新的框架软件包(5.x.x.x(。

该项目当前使用第三方属性路由框架来实现属性路由并使用诸如[GET("Route")]之类的约定,但这不是在.NET的方法[Route("Route")]中定义路由的方式。

而不是更新所有控制器和操作以使用我希望使用"桥接"属性的新约定的操作,这样我们就可以一旦项目完全转换来进行重构。麻烦是,System.Web.Mvc.RouteAttribute已密封,我无法扩展。

// compiler: GETAttribute cannot derive from sealed class RouteAttribute
public class GETAttribute : System.Web.Mvc.RouteAttribute {}

有没有办法配置框架以查找我的自定义属性路由,或者我必须重构和/或找到手动注册的方法?

非常感谢。

感谢@stephen Muecke帮助我找到这个答案。

要确保框架拾取您的属性,请在属性上使用IDirectRouteFactoryIHttpRouteInfoProvider接口:

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
public class GETAttribute : Attribute, IDirectRouteFactory, IHttpRouteInfoProvider
{
    public GETAttribute() { }
    public GETAttribute(string template)
    {
        Template = template;
    }
    public int ActionPrecedence { get; set; }
    public string Name { get; set; }
    public string Template { get; set; }
    public int Order { get; set; }
    public RouteEntry CreateRoute(DirectRouteFactoryContext context)
    {
        IDirectRouteBuilder builder = context.CreateBuilder(Template);
        builder.Name = Name;
        builder.Order = Order;
        builder.Precedence = ActionPrecedence;
        return builder.Build();
    }
}

我实际上还没有弄清楚如何指定httpmethod,但这至少在没有其他工作的情况下注册了路线。

您可以在github上的Aspnetwebstack repo上检查它的内部。

最新更新