سؤال

I have a asp.net web api application that also renders some razor pages.
By default, there are two default engines(webform\razor) and I had no problems rendering my razor pages.

Now I need to support some old aspx\ascx pages that are rendered using a custom engine.

So when I bootstrap my application, I do this:

    // Remove the default web form engine
    ViewEngines.Engines.Remove(ViewEngines.Engines.OfType<WebFormViewEngine>().FirstOrDefault());
    // Add my custom engine
    ViewEngines.Engines.Add(ApplicationContainer.Resolve<CustomViewEngine>());

My custom view engine's prototype is:

public class CustomViewEngine: VirtualPathProviderViewEngine

Now, my problem is with the old razor pages, they get rendered with this engine for some reason(and not with the razor view engine) and I get an exception while the 'FindView' function runs.

I've render my razor pages in a special way, but bottom line, it looks like this:

public ActionResult MyAction()
{
   return View('Razor/abcd.cshtml',model);
}

I've read that the web forms engine runs first and only after that the razor one runs, but I'm not sure that's correct.
I've tried returning null and other things from the custom engine, but the page doesn't get rendered.

Why does my .cshtml path get rendered with the custom engine and not with the razor engine?
How can I tell the custom engine to pass over files that end with .cshtml?

هل كانت مفيدة؟

المحلول

Ok, I've found a workaround - returning false from the "FileExists" function will go to the next file engine:

 protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
        {
            try
            {
                return !virtualPath.EndsWith("cshtml") &&
                    File.Exists(controllerContext.HttpContext.Server.MapPath(virtualPath));
            }
            catch (HttpException exception)
            {
                if (exception.GetHttpCode() != 0x194)
                {
                    throw;
                }
                return false;
            }
            catch
            {
                return false;
            }
        }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top