0

We are having problems with partial views after migrating to .net 5. The main page will optionally render a custom partial view to allow per-customer customizations. After migrating we are unable to render these partial views. the error message is:

InvalidOperationException: The partial view ~/Resources/Customer/Views/File.cshtml was not found. The following locations were searched: ~/Resources/Customer/Views/File.cshtml

We know the path is valid because if we rename the file the page renders (without customizations). If we debug in visual studio the page renders as expected but in a deployed IIS server it fails. We suspect there is a permissions issue but we have been unable to find it. So far we haven't been able to find anything in google searches or SO

Here is a snippet where we handle the custom file:

// load file path from config
string url = configuration.OrderPageSetting["OrderInfoViewRenderFile"];
bool exists = false;

// determine the path
if (!string.IsNullOrWhiteSpace(url))
{
    string _url = url;
    if (_url.StartsWith("~/")) _url = _url.Substring(2);
    else if (_url.StartsWith("/")) _url = _url.Substring(1);
    exists = System.IO.File.Exists(System.IO.Path.Combine(Resource.SystemDirectory, _url));
}

// test for and render custom view
if (exists)
{
    var data = new ViewDataDictionary<object>(new EmptyModelMetadataProvider(), new ModelStateDictionary());
    data.Add("Order", order);
    data.Add("Config", configuration);
    await Html.RenderPartialAsync(url, data);
}
else
{
... default rendering ...
}

The application is hosted in a virtual directory with a dedicated application pool. This problem occurs without a web.config at the site level. On the same server, the prior .net framework version of the application works correctly.

here is the applications web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\myapp.dll" stdoutLogEnabled="false" hostingModel="InProcess" stdoutLogFile=".\logs\stdout">
        <environmentVariables>
          <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>
Justin
  • 1,303
  • 15
  • 30
  • Could you please share the IIS site's folder? Do you host your application as a specific web application or a nested web sites or virtual directory ? – Brando Zhang Oct 08 '21 at 05:53
  • It is hosted as a web application under a virtual directory with a dedicated applicaiton pool. the Site folder does not have a web.config currently defined (i tried removing it just in case). I will put a copy of apps web.config in the original post. – Justin Oct 08 '21 at 13:10
  • Could you please share the IIS management console for this site? It seems you put this application inside a virtual directory, I suggest you could try to put this site inside a specific web application inside IIS and try again. – Brando Zhang Oct 11 '21 at 06:29

1 Answers1

0

A co-worker found the solution the problem, the clue was in https://weblog.west-wind.com/posts/2019/Sep/30/Serving-ASPNET-Core-Web-Content-from-External-Folders

There were 2 changes required:

  1. Add package: Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
  2. called extension to support runtime complication on the controllers/views:
    services.AddControllersWithViews().AddRazorRuntimeCompilation(opt =>
    {
        opt.FileProviders.Add(
            new PhysicalFileProvider(System.IO.Path.Combine(Environment.ContentRootPath, ""))
        );
    })

if you want to change where the dynamic compilation is supported, simply change the "" to the path where it is permitted.

Justin
  • 1,303
  • 15
  • 30