9

I have heard that you can use views in a class library now in MVC5.

However I can't seem to find any guide on how to actually do this.

I have created a web app, created a class library and installed the MVC package in that library. I have a controller and a view in the library and I have referenced the library from the web app. This doesn't work and to be honest I didn't expect it to. It feels like I'm missing another link that ties my app to the view in my library.

How do I get the app to look in the library for views?

Jon
  • 3,173
  • 3
  • 39
  • 66
  • Will this allow me to search class libraries? It just seems to allow me to add extra paths in which I might have stored my views. It doesn't seem to reference anything about looking for views actually stored in a class library? – Jon Feb 02 '14 at 19:14
  • Not sure, just wanted to link that answer for you to make that judgement call. – PW Kad Feb 02 '14 at 19:19
  • I don't see anything in the release notes for MVC5 that gives an indication that this is a feature in MVC5. Are you sure it's part of MVC5 release? http://www.asp.net/visual-studio/overview/2013/release-notes#TOC10 – Shiva Feb 02 '14 at 19:20
  • Not sure, Shiva, as I say it's just something I'd heard. I've also seen articles such as this (http://mhammadchehab.com/wordpress/2013/12/enabling-intellisense-for-razor-in-class-library-mvc-5-razor-3-0/) which talks about adding intellisense to razor views in a class library, making me think this is possible. – Jon Feb 02 '14 at 19:27
  • 2
    Searching for a view in a library is quite different from searching in a custom location. This has been mistakenly marked as duplicate by people who didn't understand the problem. – bug-a-lot Jan 06 '16 at 13:49
  • It is also worth noting that while previous MVC versions didn't support it out of the box, MVC 5 supports at least having view templates in a library. Partial views still seem to be troublesome. Then again, maybe partial views don't make sense to be shared across web applications. – bug-a-lot Jan 06 '16 at 13:55

1 Answers1

5

Here is a good blog post that will show you how:

http://www.danielroot.info/2013/07/reuse-mvc-views-using-virtual-path.html

Or you can use the Razor Generator project from codeplex:

http://razorgenerator.codeplex.com/

In order to get intellisense you need to make sure you have a web.config in the folder with the views.

CD Waddell
  • 248
  • 1
  • 13