简体   繁体   中英

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.

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. This way each MVC project can reference this assembly and reuse it.

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. Another way is to use a NuGet which will also contain the assembly with the shared code. 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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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