Quiero descargar e instalar el motor razor view para ASP.Net MVC 2. ¿Desde donde puedo descargarlo e instalarlo?Cómo descargar Razor View Engine
Respuesta
Posiblemente un poco anticuado, pero que tal vez debería echar un vistazo a este post.
http://stefan.rusek.org/Posts/Using-Razor-with-ASP-NET-MVC-in-Four-Easy-Steps/26/
Razor es capaz de ejecutar de manera independiente es totalmente posible para proporcionar el cableado usted mismo. Ya sea que necesite o no usar .NET4 o no, no estoy 100% seguro de hacerlo. En otra nota, MVC3 está en estado RC y tiene una licencia Go-Live que te permite comenzar a usar técnicamente ahora y cuando el RTM llega (pronto) a una actualización.
Como se ha mencionado, aún puede obtener el motor de vista en las versiones <3. –
Utilizando Matthew Abbott's y My Razor View Engine de Codeplex. Podrías hacer lo siguiente. No es compatible con modelos o páginas de diseño, pero te llevará en la dirección correcta si eliges seguir esta ruta. Francamente, simplemente actualizaría a MVC 3 cuando el tiempo esté listo. Este código se basa en mi post blog en http://buildstarted.com/2010/11/22/making-your-own-viewengine-with-markdown/
Vas a tener que añadir la siguiente línea a su global.asax:
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine.RazorViewEngine());
a menos que desee ambos motores en cuyo caso simplemente quitar Clear()
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Hosting;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace RazorViewEngine {
/// <summary>
/// ViewEngine for the RazorView. Provides basic file handling to load views.
/// </summary>
public class RazorViewEngine : IViewEngine {
string[] SearchLocations { get; set; }
Tuple<string, string, RazorView> Cache { get; set; }
VirtualPathProvider VirtualPathProvider { get; set; }
public RazorViewEngine() {
//{1} == Controller name
//{0} == View name
SearchLocations = new string[] {
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml",
};
VirtualPathProvider = HostingEnvironment.VirtualPathProvider;
}
#region IViewEngine Members
public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache) {
return CreateView(controllerContext, partialViewName, null, null, useCache);
}
public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) {
return CreateView(controllerContext, viewName, masterName, GetLayoutPath(controllerContext), useCache);
}
/// <summary>
/// Meat of the FindView methods.
/// </summary>
/// <param name="controllerContext">The current controller context for this request.</param>
/// <param name="viewName">The requested view name. </param>
/// <param name="masterName">The master page view name (currently unused)</param>
/// <param name="layoutPath">The layout path location (Replaces the masterpage in other view engines)</param>
/// <param name="useCache">Cache the viewpage?</param>
/// <remarks>The layout path is currently hardcoded to "Layout" and will look in the SearchLocations for that path</remarks>
/// <returns>Returns a ViewEngineResult with the requested view</returns>
public ViewEngineResult CreateView(ControllerContext controllerContext, string viewName, string masterName, string layoutPath, bool useCache) {
//grab the current controller from the route data
string controllerName = controllerContext.RouteData.GetRequiredString("controller");
//for proper error handling we need to return a list of locations we attempted to search for the view
string[] SearchedLocations;
//get the actual path of the view - returns null if none is found
string viewPath = GetViewPath(viewName, controllerName, out SearchedLocations);
if (viewPath != null) {
RazorView view = new RazorView(this, controllerContext, viewPath, layoutPath);
return new ViewEngineResult(view, this);
}
//we couldn't find the view - return an array of all locations we've looked in
return new ViewEngineResult(SearchedLocations);
}
/// <summary>
/// Look for the view in the current file system
/// </summary>
/// <param name="viewName">The name of the View you're looking for</param>
/// <param name="controllerName">Current controller name</param>
/// <param name="SearchedLocations">out a list of locations searched</param>
/// <returns>A string value of the relative path</returns>
public string GetViewPath(string viewName, string controllerName, out string[] SearchedLocations) {
return FindPath(viewName, controllerName, out SearchedLocations);
}
/// <summary>
/// Look for the view in the current file system
/// </summary>
/// <param name="viewName">The name of the View you're looking for</param>
/// <param name="controllerName">Current controller name</param>
/// <param name="SearchedLocations">out a list of locations searched</param>
/// <returns>A string value of the relative path</returns>
public string FindPath(string viewName, string controllerName, out string[] SearchedLocations) {
SearchedLocations = new string[SearchLocations.Length];
for (int i = 0; i < SearchLocations.Length; i++) {
string virtualPath = string.Format(SearchLocations[i], viewName, controllerName);
SearchedLocations[i] = virtualPath;
//check the active VirtualPathProvider if the file exists
if (VirtualPathProvider.FileExists(virtualPath)) {
//add it to cache - not currently implemented
return VirtualPathProvider.GetFile(virtualPath).VirtualPath;
}
}
return null;
}
/// <summary>
/// Get the layout virtual path
/// </summary>
/// <param name="controllerContext">The current Controller context for this request</param>
/// <returns>A string virtual path</returns>
public string GetLayoutPath(ControllerContext controllerContext) {
//This should probably be added to a list of locations - I'm not sure exactly
//what I need to do with this yet.
string[] locations;
return FindPath("Layout", controllerContext.RouteData.GetRequiredString("controller"), out locations);
}
/// <summary>
/// Current irrelevant
/// </summary>
/// <param name="controllerContext">The active controller context</param>
/// <param name="view">View to release</param>
public void ReleaseView(ControllerContext controllerContext, IView view) {
IDisposable disposable = view as IDisposable;
if (disposable != null) {
disposable.Dispose();
}
}
#endregion
}
/// <summary>
/// Implements IView and renders a Razor
/// </summary>
public class RazorView : IView {
ControllerContext ControllerContext;
string ViewPath;
string LayoutPath;
RazorViewEngine Engine;
public RazorView(RazorViewEngine engine, ControllerContext controllerContext, string viewPath, string layoutPath) {
//load the file
this.ControllerContext = controllerContext;
this.ViewPath = viewPath;
this.LayoutPath = layoutPath;
this.Engine = engine;
}
#region IView Members
/// <summary>
/// Converts Razor to html and writes it to the passed in writer
/// </summary>
/// <param name="viewContext"></param>
/// <param name="writer"></param>
public void Render(ViewContext viewContext, System.IO.TextWriter writer) {
//View contents
string contents = new StreamReader(VirtualPathProvider.OpenFile(ViewPath)).ReadToEnd();
string layoutContents = LayoutPath == null
? null
: new StreamReader(VirtualPathProvider.OpenFile(LayoutPath)).ReadToEnd();
contents = Parse(contents);
string output;
output = contents;
writer.Write(output);
}
/// <summary>
/// Converts Razor to html
/// </summary>
/// <param name="Razor">Razor text</param>
/// <returns>Html formatted Razor text</returns>
string Parse(string Razor) {
//Where do I get the model From
return RazorEngine.Razor.Parse(Razor);
}
#endregion
}
}
He configurado un motor de vistas bastante diferente basado en parte en su código (http://tqcblog.com/2011/01/23/precompiled-razor-for-shared-views-and-mono/). El modelo de soporte, los diseños, etc. es bastante más fácil si utiliza la precompilación para reducir las dependencias de tiempo de ejecución. No he probado la combinación mvc2/.net3.5, pero debería funcionar sin mucho más que algunos cambios de referencia. –
¡La mejor manera de obtener un motor MVC Razor, y aprender a usarlo es de Scot Gu! El gurú de todo el proyecto Razor. Su entrada en el blog http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx es muy fácil de seguir y le enseña cómo administrar el proceso de andamio desde Package Manager Consel en VS.
- 1. Razor View Engine y jQuery
- 2. MVC 3 ASPX VS RAZOR View Engine
- 3. Razor view engine, ¿cómo escribir entre html?
- 4. Cómo obtener IntelliSense para Razor View Engine
- 5. Operaciones anidadas con Razor View Engine
- 6. OutputCache in Razor view engine .cshtml página
- 7. ¿Cómo usar Razor View Engine en una aplicación de consola?
- 8. Razor view engine - ¿Cómo puedo agregar vistas parciales?
- 9. Crear un proyecto Mvc2 con Razor View Engine
- 10. Javascript Intellisense en las páginas secundarias de Razor View Engine
- 11. ¿Qué hay de nuevo en Razor view engine 2.x?
- 12. Razor view engine intelisense no funciona VS 2010
- 13. ¿Es posible usar Razor View Engine fuera de asp.net
- 14. ¿Funciona el motor Razor View Engine para Mono?
- 15. Nustache View Engine ArrayTypeMismatchException
- 16. Motor ASP.NET MVC Razor view
- 17. ¿Cómo puedo definir los usos en MVC 3 Razor View Engine en todo el sitio?
- 18. Usando Razor view engine: ¿cómo formateo un valor decimal para tener comas y dos decimales?
- 19. Nancy - Super Simple View Engine: anidado @Each
- 20. cadenas de codificación HTML - ASP.NET Web Forms VS Razor View Engine
- 21. Variables globales en el motor Razor View
- 22. ASP.NET MVC 3 Razor View Restrictions
- 23. ASP.NET MVC 4 y Razor 2: View Engine ya no es compatible con xml?
- 24. Con el nuevo Razor View Engine, ¿deberían mis HtmlHelpers devolver string o IHtmlString?
- 25. ¿Dónde se encuentra el código fuente del Razor View Engine para ASP.NET MVC?
- 26. Cuáles son algunas de las ventajas de Razor view engine sobre otros motores de vista
- 27. instalando el motor razor view para Visual Studio
- 28. Comentarios HTML en Spark view engine
- 29. Spark-View-Engine con ASP.NET MVC2
- 30. ASP.NET MVC View Engine Secuencia de resolución
System.Web.Razor * does * target v 4.0 del framework. Dado que el RTM de MVC3 es inminente, estoy de acuerdo en que tratar de conectar Razor en una aplicación MVC2 parece un poco inútil en esta etapa. –