I am currently trying to implement Razor Web Pages in older WebForms project, and also, make it possible to render partial views from string (taken from database elsewhere). I've implemented custom VirtualPathProvider with all overrides specified here: ASP.NET MVC and Virtual views and also overwritten these methods:

public override CacheDependency GetCacheDependency(string virtualPath, IEnumerable virtualPathDependencies, DateTime utcStart)
{
     if (virtualPath.Contains("RazorMigration.cshtml") && HttpContext.Current.Items.Contains("RazorTestingPage"))
     {
          return null;
     }
     return Previous.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
}

public override String GetFileHash(String virtualPath, IEnumerable virtualPathDependencies)
{
    if (virtualPath.Contains("RazorMigration.cshtml") && HttpContext.Current.Items.Contains("RazorTestingPage"))
    {
        return Guid.NewGuid().ToString();
    }

    return Previous.GetFileHash(virtualPath, virtualPathDependencies);
}

Then when I am trying to actually render page (completeTemplate already contains pure HTML with razor already parsed) like this:

var rt = new RouteData();
rt.Values.Add("controller", "WebFormShimController");

var httpCtx = new HttpContextWrapper(System.Web.HttpContext.Current);
var ctx = new ControllerContext(new RequestContext(httpCtx, rt), new WebFormShimController());
try
{
     HttpContext.Current.Items.Add("RazorTestingPage", completeTemplate);

     IView view = ViewEngines.Engines.FindPartialView(ctx, System.IO.Path.GetFileName("RazorMigration")).View;

     ViewContext vctx = new ViewContext(ctx, view,
         new ViewDataDictionary { Model = model },
         new TempDataDictionary(), httpCtx.Response.Output);
     view.Render(vctx, System.Web.HttpContext.Current.Response.Output);
 }

I always catch Exception On view.Render line saying this:

c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\0e7dfb6a\c63cc9d1\App_Web_razormigration.cshtml.afd51fd3.jujzzimy.0.cs(41): error CS1009: Unrecognized escape sequence

I am not really sure what is the issue here, or where and how this path is constructed. If someone could point me in the right direction I would be very happy as I am trying to get it working for almost a week, but still no success.

Related posts

Recent Viewed