Is it possible to reuse partial views on multiple projects in ASP.NET MVC?
Asked Answered
B

2

5

I know ASP.NET MVC 3 doesn't support area reuse, which would be very handy for, say, user admin areas of web applications, but how about partial views?

Say I have Pager "control" as a Razor (or WebFormViewEngine, it doesn't matter) partial view which I can easily reuse inside my MVC application.

Is it possible to reuse it in multiple MVC applications other than by creating a partial view in the new application and copy-pasting the code?

Bonnard answered 8/3, 2011 at 22:53 Comment(0)
C
7

There is nothing buit-in the framework that allows you to do this. You may take a look at MVCContrib portable areas which allows you to embed and reuse views between multiple ASP.NET MVC applications. You may also find the following blog post useful.

Disclaimer: both those approaches rely on writing a custom VirtualPathProvider which doesn't work with precompiled ASP.NET applications. So if you intend to precompile your application before shipping don't use those. Personally I am in this situation and what I ended up doing is writing a custom NuGet package which contains all the necessary views and assemblies containing their respective view models and all that a developer has to do is to install the NuGet package from a centralized location withing the intranet/internet.

Collegium answered 8/3, 2011 at 22:55 Comment(2)
does not it work at all? chrisvandesteeg.nl/2010/11/22/…Rotor
i tried to use that manual and realize that it's not work witn asp mvc 4 and studio 2012 so i recompile project form git hub with new asseblies (asp mvc 4) and change vie wbuilder to RazorGenerator next one i've applied patch for asp issues but it still doesn't workRotor
P
4

In the O'Reilly book "Programming ASP.NET MVC 4" by Jess Chawick there is a chapter describing what you need. "CHAPTER 15 - Reusable UI Components"

Basically you create Class Library project with your views. You have to install RazorGenerator and set it as Custom Tool in the properties of the .cshtml files. This will generate C# code from the .cshtml files. Now to locate the views outside your standard search path in your MVC application you have to use the Nuget Package PrecompiledMvcEngine.

The book is very well written and you can find step by step info how to do it.

Plectron answered 26/3, 2014 at 13:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.