1

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.

EDIT: I found the bug - It was in my encoding when writing to file (so when file was written \NUL characters were added, which confused IIS) Now I am getting this error:

CS0115: c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\0e7dfb6a\c63cc9d1\App_Web_4b7b71f4-87c2-4364-be0e-19ec2a81ceccR.0.cs.Execute()': no suitable method found to override

I am sure I don't have error in my view as it only contains this:

<h1>Hello</h1>
1
  • After hours of digging through Mvc dll, I found out where is the exact problem - it is in temporary file created by IIS, because it has something like this \NUL<\NULh\NUL1\NUL> representing <h1>, does anyone know how to deal with this?
    – John
    Oct 15, 2015 at 9:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.