0
votes

I'm really starting to lose hair here...

I'm using the open source Razor engine to use Razor templating in my Webforms solution. I have one view (.cshtml) which I compile. Looks like this:

Sample.cshtml

@inherits RazorEngine.Templating.TemplateBase
<h1>hoi</h1>

I receive the following error and have NO clue on where to look... Any leads?

CS0103: The name 'Context' does not exist in the current context

Compiler source:

Line 37:         protected ASP.global_asax ApplicationInstance {
Line 38:             get {
Line 39:                 return ((ASP.global_asax)(Context.ApplicationInstance));
Line 40:             }
Line 41:         }

Line 1:    #pragma checksum "D:\local\SitecorePackageTesting\Test5\razor\Sample.cshtml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "F5A1E4ABBCBE9FEB2C5C3D7DA7B81AC7"
Line 2:    //------------------------------------------------------------------------------
Line 3:    // <auto-generated>
Line 4:    //     This code was generated by a tool.
Line 5:    //     Runtime Version:4.0.30319.225
Line 6:    //
Line 7:    //     Changes to this file may cause incorrect behavior and will be lost if
Line 8:    //     the code is regenerated.
Line 9:    // </auto-generated>
Line 10:   //------------------------------------------------------------------------------
Line 11:   
Line 12:   namespace ASP {
Line 13:       using System;
Line 14:       using System.Collections.Generic;
Line 15:       using System.IO;
Line 16:       using System.Linq;
Line 17:       using System.Net;
Line 18:       using System.Web;
Line 19:       using System.Web.Helpers;
Line 20:       using System.Web.Security;
Line 21:       using System.Web.UI;
Line 22:       using System.Web.WebPages;
Line 23:       using System.Web.Mvc;
Line 24:       using System.Web.Mvc.Ajax;
Line 25:       using System.Web.Mvc.Html;
Line 26:       using System.Web.Routing;
Line 27:       
Line 28:       
Line 29:       public class _Page_razor_Sample_cshtml : RazorEngine.Templating.TemplateBase {
Line 30:           
Line 31:   #line hidden
Line 32:   
Line 33:           
Line 34:           public _Page_razor_Sample_cshtml() {
Line 35:           }
Line 36:           
Line 37:           protected ASP.global_asax ApplicationInstance {
Line 38:               get {
Line 39:                   return ((ASP.global_asax)(Context.ApplicationInstance));
Line 40:               }
Line 41:           }
Line 42:           
Line 43:           public override void Execute() {
Line 44:   
Line 45:   WriteLiteral("<h1>hoi</h1>");
Line 46:   
Line 47:   
Line 48:           }
Line 49:       }
Line 50:   }
Line 51:   

Compiler output:

C:\Windows\SysWOW64\inetsrv> "C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe" /t:library /utf8output /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.ApplicationServices\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.ApplicationServices.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\3c7fdb05\0071c9c6_144bca01\Oracle.DataAccess.DLL" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Activities\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.Activities.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\f5a0c252\00398325_277fcb01\Newtonsoft.Json.DLL" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\da38f160\00c98b4e_059ecb01\ITHit.WebDAV.Server.DLL" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\9cecdc75\007560c1_267fcb01\Stimulsoft.Base.DLL" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\8a7d9672\5208376d_9d10cc01\RazorForSitecore.DLL" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Services\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.Services.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Data.Linq.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\a806f09a\00c4d72a_20a1cb01\Sitecore.Publishing.WebDeploy.DLL" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel\v4.0_4.0.0.0__b77a5c561934e089\System.ServiceModel.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\5cb7410e\00101326_20a1cb01\Sitecore.Client.DLL" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\ffa50f50\001e3a2d_20a1cb01\Sitecore.Zip.DLL" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\28562d1f\006a7528_20a1cb01\sitecore.nexus.DLL" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\dd89a851\0057df41_164bca01\Mvp.Xml.DLL" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.DynamicData\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.DynamicData.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\4c2d1e9c\08f628bf_8710cc01\RazorEngine.DLL" /R:"C:\Windows\Microsoft.Net\assembly\GAC_32\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\071b6ade\00398325_277fcb01\Stimulsoft.Report.Web.DLL" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\93ebae80\00398325_277fcb01\ComponentArt.Web.UI.DLL" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ComponentModel.DataAnnotations\v4.0_4.0.0.0__31bf3856ad364e35\System.ComponentModel.DataAnnotations.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Extensions\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.Extensions.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\e04df5ec\d65f8d4c_d30fcc01\CompiledDomainModel.DLL" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\21aafcf9\0057df41_164bca01\RadEditor.Net2.DLL" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\470febb1\00398325_277fcb01\Stimulsoft.Report.WebDesign.DLL" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\cf7e7f0f\00be0505_904eca01\Lucene.Net.DLL" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\App_Code.gztscyme.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.IdentityModel\v4.0_4.0.0.0__b77a5c561934e089\System.IdentityModel.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\531c473b\0035ec2a_154bca01\HtmlAgilityPack.DLL" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\38a9e22a\00aca609_2796cb01\Telerik.Web.UI.DLL" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Helpers\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.Helpers.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Abstractions\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.Abstractions.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.Routing\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.Routing.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\33b9b8c0\007560c1_267fcb01\Stimulsoft.Report.DLL" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\1b84e797\00f1082c_20a1cb01\Sitecore.Update.DLL" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Deployment\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Deployment.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.WorkflowServices\v4.0_4.0.0.0__31bf3856ad364e35\System.WorkflowServices.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Web\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.Web.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\d8d920fa\0057df41_164bca01\RadSpell.Net2.DLL" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\ad9c306b\00101326_20a1cb01\Sitecore.Analytics.DLL" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\1e6a806e\007560c1_267fcb01\Stimulsoft.Database.DLL" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Serialization\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Activation\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.Activation.dll" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Razor\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Razor.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\App_global.asax.epwwf_0j.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\29ec3d62\003d4427_20a1cb01\Sitecore.Kernel.DLL" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\515b39c8\eb89866d_9d10cc01\Test5.DLL" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\5dc738c0\0097a629_20a1cb01\Sitecore.NVelocity.DLL" /R:"C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\1d85ead0\00c4d72a_20a1cb01\Sitecore.Oracle.DLL" /R:"C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Activities\v4.0_4.0.0.0__31bf3856ad364e35\System.Activities.dll" /R:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\assembly\dl3\d38bbdbe\006a7528_20a1cb01\Sitecore.Logging.DLL" /out:"C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\App_Web_sample.cshtml.24833d72.4mrrqd0w.dll" /D:DEBUG /debug+ /optimize- /w:4 /nowarn:1659;1699;1701 /warnaserror- "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\App_Web_sample.cshtml.24833d72.4mrrqd0w.0.cs" "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\App_Web_sample.cshtml.24833d72.4mrrqd0w.1.cs"

Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1 Copyright (C) Microsoft Corporation. All rights reserved.

c:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\d16e214a\21d2630\App_Web_sample.cshtml.24833d72.4mrrqd0w.0.cs(39,43): error CS0103: The name 'Context' does not exist in the current context

2

2 Answers

3
votes

You're trying to use a RazorEngine specific template as the base template type for a view. This won't work as TemplateBase doesn't inherit from System.Web.WebPages.WebPageBase. Essentially you are using a .cshtml file and calling it directly, whereas you'd need to do something like:

string template = File.ReadAllText(Server.MapPath("~/path/to/template.cshtml"));
string result = Razor.Parse(template);

You don't need to worry about the @include statement at the top of the file either, the TemplateBase base type is the default template base type, unless you want to use a model, in which case the default is TemplateBase<T>

The key thing to take away from this is that we didn't design RazorEngine to be a replacement of the MVC RazorViewEngine, nor the WebPages build provider, but to provide a simplistic templating solution.

0
votes

The problem was related to the Webforms based CMS i use. It tried to load the Razor file as Usercontrol which obviously isn't possible...