【问题标题】:Where to put filter, attribute and extension in ASP.NET MVC在 ASP.NET MVC 中放置过滤器、属性和扩展的位置
【发布时间】:2011-05-19 21:11:17
【问题描述】:

我刚刚读完 Rick Anderson 关于保护您的ASP.NET MVC application 的文章。在文章中,他谈到了创建新的过滤器属性。

public class LogonAuthorize : AuthorizeAttribute 
{
     public override void OnAuthorization(AuthorizationContext filterContext) 
     {
         if (!(filterContext.Controller is AccountController))
             base.OnAuthorization(filterContext);
     }
}

using System;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class AllowAnonymousAttribute : Attribute { }

我的问题是,组织这些过滤器和属性的最佳方式是什么?我是否为每个创建一个新的类文件?我是否为所有过滤器和属性创建一个类文件?放在哪里最好?在根文件夹?在子文件夹中?

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    我们有一个 MVCFilters 目录,每个属性过滤器都有自己的类。

    如果我们开始创建大量自定义过滤器,我们可能会将它们分解为子目录,例如安全性和我们拥有的任何其他内容。

    【讨论】:

    • 我从文章中下载了示例,它的作用与您描述的一样。谢谢!
    • 如果我使用区域,并创建在区域中使用的自定义过滤器,那么 MVCFilters 目录存在于根目录或该区域中?
    • 如果它的区域是特定的,它会进入该区域。
    • 谢谢@ShaneCourtrille 所以它在 Areas/Admin/Model/MVCFilters 或 Areas/Admin/MVCFilters 中?
    • 我会把它放在 Areas/Admin/MVCFilters 中,因为它不是模型。
    猜你喜欢
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多