asp.net-mvcasp.net-core-mvc.net-standard-2.0filecontentresult

Creating a .NET Standard library that uses other MVC libraries


I am trying to create a .NET Standard 2.0 library that is to be used by multiple MVC applications which may be .NET Framework or .NET Core. This particular library is supposed to export data as an excel file. To do this I need to use FileContentResult which is part of Microsoft.AspNetCore.Mvc(Core). I do not have options to install the Framework version of this library.

How would I go about implementing FileContentResult in my .NET Standard library so that it will work in both a Core or Framework application?


Solution

  • Your library should not be working with something like FileContentResult. The single-responsibility principle dictates that it should handle only the functionality that's actually relevant to its purpose, namely creating an excel file from some set of data. You can then return this from your library as a Stream.

    Whatever, then, is using your library will decide what to do with that Stream. In the case on an ASP.NET Core app, you might return it as a FileStreamResult. In another scenario, you might write it to the filesystem, or add it as an attachment to an email, etc. Tying it to one narrow scenario such as returning a file as response from a web app limits the usability of your library.

    Long and short, the answer to your question is essentially you don't need to do this in the first place. Keep your library focused on what it's actually responsible for, and you don't need these types of dependencies.