Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RazorViewEngine.FindPartialView useCache. What does it do?

I need to render a partial view to string, so I can render a Razor syntax partial view into an Asp.net view. Part of the process involves getting the view engine to find the view:

var engine = 
    ViewEngines.Engines
        .OfType<RazorViewEngine>()
        .SingleOrDefault();
if(engine == null)throw new Exception("no razor viewengine");
//useCache is the last parameter in the invocation below
var viewResult = engine
    .FindPartialView(htmlHelper.ViewContext, viewName, false);

If I set the useCache parameter to true (i.e. I don't want the virtual path provider to search for it every time I ask for it), the view is no longer found. I would have expected that if it's not found via cache, we'd fall back to non-cached method.

Are my expectations wrong? Is this something to do with mixing ViewEngines?

Confusing me further, if we simply ask the ViewEngine.Engines collection to find the view, we're not given a useCache parameter:

var viewResult = 
    ViewEngines
         .Engines
         .FindPartialView(htmlHelper.ViewContext, viewName);

I think I'll go with this FTTB, but it begs the question... why the discrepancy?

like image 246
spender Avatar asked Mar 24 '26 00:03

spender


1 Answers

The FindView and the FindPartialView methods are internally called by the ASP.NET MVC framework with useCache=true first, then useCache=false if nothing is found. You could try doing the same.

Confusing me further, if we simply ask the ViewEngine.Engines collection to find the view, we're not given a useCache parameter:

That's because this method does what I explained previously.

like image 80
Darin Dimitrov Avatar answered Mar 25 '26 14:03

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!