从WebAPI请求获取路由方法的特定属性

时间:2015-09-29 14:02:53

标签: c# asp.net-web-api asp.net-web-api2 asp.net-web-api-routing

我有一个Controller,它实现了AuthorizableApi类。

[AttributeUsage(AttributeTargets.Method)]
public class AuthorizableRoute : Attribute { }

public class AuthorizableApi : ApiController
{
    public override Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
    {
        /* Add functionality here to secure the API */
        return base.ExecuteAsync(controllerContext, cancellationToken);
    }
}

这允许我保护整个控制器,但是我也希望能够保护单个动作。 使用controllerContext我可以获得控制器和路由,但我不知道是否可以获得该操作的属性。

    [HttpPost]
    [AuthorizableRoute]
    public HttpResponseMessage DataAction([FromBody] DataType data)
    {
        /* Actions */
    }

所以,我希望能够做点像......

if(Attribute.GetCustomAttribute(myRoute, typeof (AuthorizableRoute)) != null) { }

如果这不可能,那么什么是可行的替代方案?

1 个答案:

答案 0 :(得分:1)

您可以通过实施过滤器来实现此目的。但是,您应该继承AuthorizationFilterAttribute并实现OnAuthorization(HttpActionContext context)方法。 它应该是这样的:

var loader = new THREE.TextureLoader();
var url = 'assets/images/particles/cloudSml.png';
var texture = loader.load( url );

var particleGroupCrash = new SPE.Group({
    texture: {
        value: texture
    },
    blending: THREE.NormalBlending
});

var crashemitter = new SPE.Emitter({

    maxAge: { value: 12 },
    position: { 
        value: new THREE.Vector3( 0, 0, 0 ),
        spread: new THREE.Vector3( 1, 0.5, 2 ),
    },
    size: {
        value: [ 2, 8 ],
        spread: [ 0, 1, 2 ]
    },
    acceleration: {
        value: new THREE.Vector3( 0, 0, 0 ),
    },
    rotation: {
        axis: new THREE.Vector3( 0, 1, 0 ),
        spread: new THREE.Vector3( 0, 20, 0 ),
        angle: 100 * Math.PI / 180,
    },
    velocity: {
        value: new THREE.Vector3( 0, 1, -0.5 ),
        spread: new THREE.Vector3( 0.25, 0.1, 0.25 )
    },
    opacity: {
        value: [ 0.2, 0.5, 0 ]
    },
    color: {
        value: [ new THREE.Color( 0x333333 ), new THREE.Color( 0x111111 ) ],
        spread: [ new THREE.Vector3( 0.2, 0.1, 0.1 ), new THREE.Vector3( 0, 0, 0 ) ]
    },
    particleCount: 600,
});