【问题标题】:Custom Attribute for the route路由的自定义属性
【发布时间】:2014-05-06 10:02:19
【问题描述】:

我想为我的路由中的参数创建一个自定义属性。我不能使用“ParameterBindingAttribute”

在“做某事”之前,我想验证参数 myVar

如何做到这一点?

[Route("{myVar}/TOTO")]
public IHttpActionResult Get([myAttribute]int myVar)
{
    //Do something
}

谢谢

【问题讨论】:

  • 能否请您详细说明您的问题以使其清楚?

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


【解决方案1】:

您可以编写一个动作过滤器来检查这些值。

[VerifyMyVar]
public IHttpActionResult Get(int myVar)
{
    // TODO
}

public class VerifyMyVar : HttpActionFilterAttribute
{
    ...
}

如果您想要更具体的验证,您可以让过滤器检查 MethodInfo,并使用 [myAttribute] 中的代码执行更具体的操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 2015-04-26
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多