自AuthorizeAttribute [英] Custom AuthorizeAttribute

查看:138
本文介绍了自AuthorizeAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个子类AuthorizeAttribute命名CheckArticleExistence。

I have a child class of AuthorizeAttribute named CheckArticleExistence.

我想用我收到的动作参数来设置的属性。像这样的:

I would like to set an attribute using the parameter that I receive in the action. Like this:

[CheckArticleExistence(Id=articleId)]
public ActionResult Tags(int articleId)
{
...
}

我想使用条款ArticleID检查是否存在于数据库中的文章,如果没有,我可以用OnAuthorization方法触发不同的东西。

I want to use the articleId to check if that article exists in the database, and if it doesn't I can trigger something different using the OnAuthorization method.

有什么办法?
谢谢你。

Is there any way? Thanks.

推荐答案

我觉得你可以得到的条款ArticleID 的距离的 AuthorizationContext 的,所以你不需要传递它作为属性的属性。

I think You can get the articleId from the AuthorizationContext, so you don't need to pass it as a property of the attribute.

您可以这样做:

[CheckArticleExistence]
public ActionResult Tags(int articleId)
{
...
}

这篇关于自AuthorizeAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆