Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

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

Closed
Ponant opened this issue Jun 26, 2017 · 13 comments

Comments

@Ponant
Copy link

Ponant commented Jun 26, 2017

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) OdeToCode/AddFeatureFolders#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
#6308
#6428

@pranavkm
Copy link
Contributor

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.

@Ponant
Copy link
Author

Ponant commented Jun 26, 2017

@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.

@rynowak
Copy link
Member

rynowak commented Jun 26, 2017

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

@Ponant
Copy link
Author

Ponant commented Jun 26, 2017

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

@pranavkm
Copy link
Contributor

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

@rynowak
Copy link
Member

rynowak commented Jun 26, 2017

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

@rynowak rynowak added this to the 2.0.0 milestone Jun 26, 2017
@pranavkm
Copy link
Contributor

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

@Ponant
Copy link
Author

Ponant commented Jun 27, 2017

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

@pranavkm
Copy link
Contributor

@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:

public class FixBackSlashViewLocationExpander : IViewLocationExpander
{
   public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
   {
        foreach (var path in viewLocations)
        {
            yield return path.Replace('\\', '/');
        }
   }
   
   public void PopulateValues(ViewLocationExpanderContext context){}
}

@Ponant
Copy link
Author

Ponant commented Jun 27, 2017

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

@pranavkm
Copy link
Contributor

Both of those releases.

@Ponant
Copy link
Author

Ponant commented Jun 27, 2017

OK I will try this in the meanwhile.

@Ponant
Copy link
Author

Ponant commented Jun 27, 2017

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

            services.Configure<RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new FixBackSlashViewLocationExpander());
            });

@Ponant Ponant changed the title Layout no rendered via ViewStart.cshtml: Question on Razor View discovery in .Net Core preview 1 Layout not rendered via ViewStart.cshtml: Question on Razor View discovery in .Net Core preview 1 Jun 27, 2017
@rynowak rynowak moved this from Working to Done in Razor Pages 2.0.0-preview3 Jun 29, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
No open projects
Development

No branches or pull requests

3 participants