Mvc: Layout not rendered via ViewStart.cshtml: Question on Razor View discovery in .Net Core preview 1

Created on 26 Jun 2017  路  13Comments  路  Source: aspnet/Mvc

Hi,
I was using a simple but otherwise great library in .Net Core 1.1 by Scott Allen https://github.com/OdeToCode/AddFeatureFolders/ that allows one to put controllers and views in a Feature-folder architecture. The View discovery of this NuGet was working perfectly fine in 1.1, but implementing this architecture in .Net Core 2.0 Preview 1 makes the Layout View via ViewStart.cshtml not discoverable. I tested this with an empty template from preview 1 with the minimum required (one controller one view, one shared folder hosting a layout, and one viewstart and viewimport). The workaround is to call Layout="_Layout" from the view to be rendered in RenderBody() (e.g. Index view of a Home controller) https://github.com/OdeToCode/AddFeatureFolders/issues/16.
Thus my question: is there any change that occurred to the view engine since 1.1 you can point me to and that could be at the origin of this malfunctioning? I have seen some comments but they seem to concern Razor pages only
https://github.com/aspnet/Mvc/issues/6308
https://github.com/aspnet/Mvc/issues/6428

3 - Done bug

All 13 comments

Could you share a repro app (preferably as a GitHub repo)? There haven't been too many changes to the _ViewStart discovery in regular cshtml views since 1.0, so it'd hard you to point to a specific changeset.

@pranavkm https://github.com/Ponant/LayoutFeature .
Navigate to http://localhost:56964/myroute and you should see only Index. If instead you uncomment Layout="_Layout" in the Index.cshtml, then you should see Layout and Index.

@pranavkm is there a bug here? If we're not compatible with 1.1 then this is bad.

It seems that Scott's NuGet is searching in the correct locations

Mvc\Home\_Layout.cshtml
Mvc\Shared\_Layout.cshtml

But somehow the program fails to load them unless I explicitly call them from the Home/Index View

@rynowak - we missed a path normalization (\\ -> /') when switching from ViewHierarchyUtility to RazorTemplateEngine to look for imports. I'm verifying the fix now.

So it sounds like no tooling impact right? This is just in the pieces that resolve the layout?

Yup. It's isolated to the view engine + view location expanders.

@pranavkm @rynowak , do you think that will be fixed for the second round, i.e. Preview 2?

@Ponant - we're close to signing off on our preview2 work, and its unlikely we would reset for a bug fix at this point. This should be available for 2.0.0 release though.
That said for preview2 you should be able to workaround this by inserting a view location expander, that runs after the FeatureViewLocationExpander, that replaces back slashes with forward slashes:

```C#
public class FixBackSlashViewLocationExpander : IViewLocationExpander
{
public IEnumerable ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable viewLocations)
{
foreach (var path in viewLocations)
{
yield return path.Replace('\', '/');
}
}

public void PopulateValues(ViewLocationExpanderContext context){}
}
```

@pranavkm , your workaround should work for preview 1 also, or only preview 2? Thanks

Both of those releases.

OK I will try this in the meanwhile.

To complete @pranavkm workaround, one needs to configure services as such

            services.Configure<RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new FixBackSlashViewLocationExpander());
            });
Was this page helpful?
0 / 5 - 0 ratings