使用ASP.NET MVC2的Spark-View-Engine

时间:2022-08-26 21:28:54

How do you modify a ASP.NET MVC 2.0 project to work with the Spark View Engine?

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

I tried like described here: http://dotnetslackers.com/articles/aspnet/installing-the-spark-view-engine-into-asp-net-mvc-2-preview-2.aspx

我试着像这里描述的那样:http://dotnetslackers.com/articles/aspnet/installing-the-spark-view-engine-into-asp-net-mvc-2-preview-2.aspx

But somehow it still tries to route to .aspx files.

但不知何故,它仍然试图路由到.aspx文件。

Here the code of my global.asax:

这里是我的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);
    }
}

}

5 个解决方案

#1


3  

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

I had to download the spark view engine source code (http://sparkviewengine.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27600). Once I did that I went through each of the projects that had a reference to the 1.0 version of System.Web.Mvc assembly and updated to reference to point to System.Web.Mvc 2.0. From there you can build the solution (in visual studio) and you will find that a whole bunch of tests start to fail. You can attempt to fix them (by adding the additional TextWriter parameter you will find is now needed). You will also see that the SparkView.cs file complains about a missing parameter. In the Render method (line 100 of the source code I downloaded) I had to update the instantiation of the wrappedViewContext to look like this (add writer to the end of the list of parameters):

我不得不下载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

    ...
}

Once the code is updated you can run the build.cmd script that is in the root of the source you downloaded. The build process will create a zip file in the build/dist folder. Take those new dll's and add them to your website. Things should work once again.

更新代码后,您可以运行您下载的源根目录中的build.cmd脚本。构建过程将在build / dist文件夹中创建一个zip文件。拿这些新的dll并将它们添加到您的网站。事情应该再次发挥作用。

#2


2  

You need to register the Viewengine:

您需要注册Viewengine:

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

#3


1  

If you fancy rolling your own then there is a fix on the sparkview google group.

如果您喜欢自己动手,那么sparkview google组就有了解决方法。

Personally I'd wait for the next release.

就个人而言,我会等待下一个版本。

#4


0  

My global.asax.cs contains this:

我的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());

    }
}

and my web.config contains this:

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

#5


0  

I would look at the samples comes with the Spark-1.0.zip package. Looking at one of them randomly has this in the Global.asax.cs

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

SparkEngineStarter.RegisterViewEngine();

Hope it helps.

希望能帮助到你。

#1


3  

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

I had to download the spark view engine source code (http://sparkviewengine.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=27600). Once I did that I went through each of the projects that had a reference to the 1.0 version of System.Web.Mvc assembly and updated to reference to point to System.Web.Mvc 2.0. From there you can build the solution (in visual studio) and you will find that a whole bunch of tests start to fail. You can attempt to fix them (by adding the additional TextWriter parameter you will find is now needed). You will also see that the SparkView.cs file complains about a missing parameter. In the Render method (line 100 of the source code I downloaded) I had to update the instantiation of the wrappedViewContext to look like this (add writer to the end of the list of parameters):

我不得不下载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

    ...
}

Once the code is updated you can run the build.cmd script that is in the root of the source you downloaded. The build process will create a zip file in the build/dist folder. Take those new dll's and add them to your website. Things should work once again.

更新代码后,您可以运行您下载的源根目录中的build.cmd脚本。构建过程将在build / dist文件夹中创建一个zip文件。拿这些新的dll并将它们添加到您的网站。事情应该再次发挥作用。

#2


2  

You need to register the Viewengine:

您需要注册Viewengine:

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

#3


1  

If you fancy rolling your own then there is a fix on the sparkview google group.

如果您喜欢自己动手,那么sparkview google组就有了解决方法。

Personally I'd wait for the next release.

就个人而言,我会等待下一个版本。

#4


0  

My global.asax.cs contains this:

我的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());

    }
}

and my web.config contains this:

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

#5


0  

I would look at the samples comes with the Spark-1.0.zip package. Looking at one of them randomly has this in the Global.asax.cs

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

SparkEngineStarter.RegisterViewEngine();

Hope it helps.

希望能帮助到你。