简体   繁体   English

在不同的MVC项目之间共享ActionFilter

[英]Share ActionFilter between differents MVC projects

I have to create multiple Asp .Net MVC projects and I want to share common Action Filters (among other things) between these projects in order to avoid repeating code. 我必须创建多个Asp .Net MVC项目,并且想要在这些项目之间共享常见的动作筛选器(除其他外),以​​避免重复代码。

For example: 例如:

using System.Web.Mvc;

namespace CoreMVC
{
    public class CustomHandleErrorAttribute : HandleErrorAttribute
    {
        public CustomHandleErrorAttribute() { /* ... */ }

        public override void OnException(ExceptionContext filterContext) { /* ... */ }
    }
}

The solution I have thought so far is having this class (and others) in a Class Library project with a reference to System.Web.MVC assembly. 到目前为止,我一直认为解决方案是在Class Library项目中包含此类(以及其他类), 并引用System.Web.MVC程序集。 This way each MVC project can reference this assembly and reuse it. 这样,每个MVC项目都可以引用此程序集并重新使用它。

Is this the proper way to do this or is there any other better solution? 这是执行此操作的正确方法,还是有其他更好的解决方案?

Assemblies/Class libraries is the standard way to share/reuse code in .NET applications so that's one way of doing it. 程序集/类库是在.NET应用程序中共享/重用代码的标准方法,因此,这是一种实现方法。 Another way is to use a NuGet which will also contain the assembly with the shared code. 另一种方法是使用NuGet,它还将包含带有共享代码的程序集。 The advantage of the NuGet is that you can include some additional stuff along with the class library (such as some bootstrapping Powershell scripts that perform additional customizations to the target project in which you install the NuGet). NuGet的优点是您可以在类库中包含一些其他内容(例如一些对安装NuGet的目标项目执行其他自定义的自举Powershell脚本)。 Another great advantage of a NuGet is that you can have multiple platforms in it and depending on which version/.NET platform you are installing to have the proper assembly getting referenced. NuGet的另一个巨大优点是,您可以在其中拥有多个平台,并且可以根据要安装的版本/.NET平台来引用正确的程序集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM