asp.Net   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – ActionResult上的自定义属性大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这可能是菜鸟的问题,但是

假设我有一个ActionResult,我只想在几个小时后授予访问权限.

我们还说我想用自定义属性来装饰我的ActionResult.

所以代码可能看起来像

[AllowAccess(after="17:00:00",before="08:00:00")]
public ActionResult AfterHoursPage()
{
    //Do something not so interesTing here;

    return View();
}@H_197_9@ 
 

我该如何才能得到这个工作

我已经做了一些关于创建自定义属性的研究,但是我觉得我错过了如何消耗它们.

请假设我知道几乎没有关于创建和使用它们.

解决方法

尝试这个(未经测试):
public class AllowAccessAttribute : AuthorizeAttribute
{
    public datetiR_933_11845@e before;
    public datetiR_933_11845@e after;

    protected override bool AuthorizeCore(httpContextBase httpContext)
    {
        if (httpContext == null)
            throw new ArgumentNullException("httpContext");

        datetiR_933_11845@e current = datetiR_933_11845@e.Now;

        if (current < before | current > after)
            return false;

        return true;
    }
}@H_197_9@ 
 

更多信息:
http://schotime.net/blog/index.php/2009/02/17/custom-authorization-with-aspnet-mvc/

大佬总结

以上是大佬教程为你收集整理的asp.net-mvc – ActionResult上的自定义属性全部内容,希望文章能够帮你解决asp.net-mvc – ActionResult上的自定义属性所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。