如何修改ASP.NET MVC 2.0项目以使用Spark View引擎?
我试着像这里描述的那样: http://dotnetslackers.com/articles/aspnet/installing-the-spark-view-engine-into-asp-net-mvc-2-preview-2.aspx
但不知怎的,它仍然试图路由到.aspx文件。
这是我的global.asax代码:
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
SparkViewFactory svf = new SparkViewFactory();
PrecompileViews(svf);
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
public static void PrecompileViews(SparkViewFactory svf)
{
var controllerFactory = svf;
var viewFactory = new SparkViewFactory(controllerFactory.Settings);
var batch = new SparkBatchDescriptor();
batch
.For<HomeController>()
.For<AccountController>();
viewFactory.Precompile(batch);
}
}
}
答案 0 :(得分:3)
http://www.simple-talk.com/community/blogs/asiemer/archive/2010/01/31/89132.aspx
我必须下载spark视图引擎源代码(http://sparkviewengine.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27600)。一旦我这样做,我就浏览了每个参与1.0版System.Web.Mvc程序集的项目,并更新为引用指向System.Web.Mvc 2.0。从那里你可以构建解决方案(在visual studio中),你会发现一大堆测试开始失败。您可以尝试修复它们(通过添加您现在需要的其他TextWriter参数)。您还将看到SparkView.cs文件抱怨缺少参数。在Render方法(我下载的源代码的第100行)中,我不得不更新wrappedViewContext的实例化,使其看起来像这样(将writer添加到参数列表的末尾):
public void Render(ViewContext viewContext, TextWriter writer)
{
var wrappedHttpContext = new HttpContextWrapper(viewContext.HttpContext, this);
var wrappedViewContext = new ViewContext(
new ControllerContext(wrappedHttpContext, viewContext.RouteData, viewContext.Controller),
viewContext.View,
viewContext.ViewData,
viewContext.TempData,
writer); // <-- add the writer to the end of the list of parameters
...
}
更新代码后,您可以运行您下载的源根目录中的build.cmd脚本。构建过程将在build / dist文件夹中创建一个zip文件。拿这些新的dll并将它们添加到您的网站。事情应该再次发挥作用。
答案 1 :(得分:2)
您需要注册Viewengine:
ViewEngines.Engines.Add(new SparkViewFactory());
答案 2 :(得分:1)
如果您喜欢自己滚动,那么sparkview google group就会有一个修复。
我个人会等下一个版本。
答案 3 :(得分:0)
我的global.asax.cs包含:
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
ViewEngines.Engines.Add(new SparkViewFactory());
}
}
我的web.config包含:
<configSections>
<section name="spark" type="Spark.Configuration.SparkSectionHandler, Spark"/>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/>
<section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
<section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/>
</sectionGroup>
</sectionGroup>
</sectionGroup>
</configSections>
<spark>
<pages>
<namespaces>
<add namespace="System"/>
<add namespace="System.Collections.Generic"/>
<add namespace="System.Linq"/>
<add namespace="System.Web.Mvc"/>
</namespaces>
</pages>
</spark>
答案 4 :(得分:0)
我会看看Spark-1.0.zip包附带的示例。在Global.asax.cs
中随机查看其中一个SparkEngineStarter.RegisterViewEngine();
希望它有所帮助。