How to create an ASP.NET MVC class library

13,138

Solution 1

Use NuGet to add references to the pieces of functionality, like EF and System.Web.MVC, that you need in your class library or libraries.

A data access project to handle persistence and a class library to hold HTML Helpers that you might want to reuse both make some sense. Razor views if you're using the RazorEngine rendering stack can also be interesting to be able to test.

Solution 2

you used the correct template - a simple class library is all you need.

then in the MVC web project just add a reference to the class library project

Solution 3

You are right to use the Class Library template in visual studio for your needs. You can add all of the references you need through NuGet (such as Razor, EF, and so on) and by Right clicking on references in the Solution Explorer and picking and choosing what you need.

Remember when using multiple projects that you add references between projects too! (for example your Web App project needs to know about your Data Repository Project)

Share:
13,138
Michael Itzoe
Author by

Michael Itzoe

Updated on July 26, 2022

Comments

  • Michael Itzoe
    Michael Itzoe almost 2 years

    I want to create a class library for an MVC 4 web application. Every search I've tried has returned plenty of references that merely mention creating one, or the importance of doing so, but not specifics of how.

    My first assumption was a template would be under Web in the Visual Studio New Project dialog, but no. I was unsure if I was to use the Class Library template under Windows, but did.

    I want to include things like some data access (e.g., DbContext), but while Intellisense sees the System.Data.Entity namespace, there are no classes available. I guess I need some additional references, but no idea which ones. Looking at the references in my main MVC project, at lot of them are pointing to the Packages folder. I'm unsure if I should be doing the same.

    In short, I'm looking for instructions on how to create a class library for MVC in Visual Studio, including the necessary references for EF, Razor and whatever else.