Ho modificato l'idea pubblicata here, per lavorare con MVC3. E 'stato abbastanza veloce e facile. L'unico inconveniente minore è che le viste condivise devono essere risorse incorporate e, pertanto, compilate.
Inserire le viste condivise (file .cshtml, .vbhtml) in un progetto di libreria. (Ho anche alcuni controller condivisi in questo progetto.) Se si desidera utilizzare _Layout.cshtml dall'applicazione, assicurarsi di includere un _ViewStart.cshtml, che punta ad esso, con le viste condivise.
Nel progetto di libreria, impostare tutte le proprietà 'Build Action di viste su Risorsa incorporata.
Nel progetto di libreria aggiungere il seguente codice che scriverà il contenuto delle viste in una directory tmp/Views.
.
public class EmbeddedResourceViewEngine : RazorViewEngine
{
public EmbeddedResourceViewEngine()
{
ViewLocationFormats = new[] {
"~/Views/{1}/{0}.aspx",
"~/Views/{1}/{0}.ascx",
"~/Views/Shared/{0}.aspx",
"~/Views/Shared/{0}.ascx",
"~/Views/{1}/{0}.cshtml",
"~/Views/{1}/{0}.vbhtml",
"~/Views/Shared/{0}.cshtml",
"~/Views/Shared/{0}.vbhtml",
"~/tmp/Views/{0}.cshtml",
"~/tmp/Views/{0}.vbhtml"
};
PartialViewLocationFormats = ViewLocationFormats;
DumpOutViews();
}
private static void DumpOutViews()
{
IEnumerable<string> resources = typeof(EmbeddedResourceViewEngine).Assembly.GetManifestResourceNames().Where(name => name.EndsWith(".cshtml"));
foreach (string res in resources) { DumpOutView(res); }
}
private static void DumpOutView(string res)
{
string rootPath = HttpContext.Current.Server.MapPath("~/tmp/Views/");
if (!Directory.Exists(rootPath))
{
Directory.CreateDirectory(rootPath);
}
Stream resStream = typeof(EmbeddedResourceViewEngine).Assembly.GetManifestResourceStream(res);
int lastSeparatorIdx = res.LastIndexOf('.');
string extension = res.Substring(lastSeparatorIdx + 1);
res = res.Substring(0, lastSeparatorIdx);
lastSeparatorIdx = res.LastIndexOf('.');
string fileName = res.Substring(lastSeparatorIdx + 1);
Util.SaveStreamToFile(rootPath + fileName + "." + extension, resStream);
}
}
sto usando StreamToFile scrittore di Adrian, trovato here.
- Nei Global.asax.cs dell'applicazione aggiuntivo:
.
public static void RegisterCustomViewEngines(ViewEngineCollection viewEngines)
{
//viewEngines.Clear(); //This seemed like a bad idea to me.
viewEngines.Add(new EmbeddedResourceViewEngine());
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
RegisterCustomViewEngines(ViewEngines.Engines);
}
fonte
2011-05-10 12:52:48
Ho visto il post di los techies. Sono sicuro che funziona bene per le persone, ma è un totale esagerato per ciò di cui ho bisogno. Messaggi con l'autobus e l'altra configurazione mi sembrano pesanti. La compilation della visualizzazione Razor è una impostazione di proprietà sulle viste (anche se su ogni vista) e il gioco è fatto. – blu