【问题标题】:Spark-View-Engine with ASP.NET MVC2Spark-View-Engine 与 ASP.NET MVC2
【发布时间】:2010-01-14 15:24:01
【问题描述】:

如何修改 ASP.NET MVC 2.0 项目以使用 Spark 视图引擎?

我尝试过这里描述的: 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);
    }
}

}

【问题讨论】:

  • 看来没有人能解决这个问题,让我们等待发布:(
  • 这在 ASP.NET MVC 2 的最新版本中暂时被破坏。请参阅stackoverflow.com/questions/2138583/… 了解您可以在此期间使用的解决方法。

标签: asp.net asp.net-mvc spark-view-engine


【解决方案1】:

http://www.simple-talk.com/community/blogs/asiemer/archive/2010/01/31/89132.aspx

我必须下载 spark 视图引擎源代码 (http://sparkviewengine.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27600)。完成此操作后,我浏览了每个引用 System.Web.Mvc 程序集的 1.0 版本并更新为指向 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 并将它们添加到您的网站。事情应该会再次起作用。

【讨论】:

  • 谢谢。顺便说一句,你的书很棒。
【解决方案2】:

您需要注册 Viewengine:

ViewEngines.Engines.Add(new SparkViewFactory());

【讨论】:

  • 嗯,我已经试过了。结果:找不到方法:'Void System.Web.Mvc.ViewContext..ctor(System.Web.Mvc.ControllerContext, System.Web.Mvc.IView, System.Web.Mvc.ViewDataDictionary, System.Web.Mvc .TempDataDictionary)'。
  • @ben 这只是暂时的。 SparkViewEngine 的作者正在努力修复,但他这几天有点忙。
【解决方案3】:

如果您喜欢自己滚动,那么sparkview google group 上有一个修复程序。

我个人会等待下一个版本。

【讨论】:

    【解决方案4】:

    我的 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>
    

    【讨论】:

    • 嗯,我试过了。结果:找不到方法:'Void System.Web.Mvc.ViewContext..ctor(System.Web.Mvc.ControllerContext, System.Web.Mvc.IView, System.Web.Mvc.ViewDataDictionary, System.Web.Mvc .TempDataDictionary)'。
    • 我用的是MVC2,可能MVC1不一样。
    • 您的项目是否引用了 System.Web.MVC?
    • 是的。我使用 MVC 2.0 模板创建它我再次检查 System.Web.MVC 被引用
    【解决方案5】:

    我会查看 Spark-1.0.zip 包附带的示例。随机查看其中一个在 Global.asax.cs 中有这个

    SparkEngineStarter.RegisterViewEngine();
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多