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

Should Pages search in /Pages/Shared ? #6534

Closed
ardalis opened this issue Jul 12, 2017 · 6 comments
Closed

Should Pages search in /Pages/Shared ? #6534

ardalis opened this issue Jul 12, 2017 · 6 comments

Comments

@ardalis
Copy link
Contributor

ardalis commented Jul 12, 2017

https://github.com/aspnet/Mvc/blob/a8eb5bee7023a2a5d49fcdaea0c25b3416a69211/src/Microsoft.AspNetCore.Mvc.RazorPages/Internal/RazorPagesRazorViewEngineOptionsSetup.cs

RazorPages appear to only look in /Views/Shared, not /Pages/Shared for shared view resources. Should they look in /Pages/Shared instead or as well? That seems intuitive to me, although I can understand how it could undermine the ability to use "Shared" in page-based routes. If this is by design, it should be documented.

@rynowak
Copy link
Member

rynowak commented Jul 12, 2017

If this is by design, it should be documented.

This is by design for the reasons you mentioned. We also don't document stuff we don't do, because the list would be pretty long.

If you want this behavior you can add it to PageViewLocationFormats

@ardalis
Copy link
Contributor Author

ardalis commented Jul 12, 2017

We also don't document stuff we don't do

Fair enough, but it's not obvious that pages will search for shared files in /Views/Shared. That, at least, should be documented, yes?

@ardalis
Copy link
Contributor Author

ardalis commented Jul 12, 2017

FYI, I was messing around with this and did add this:

services.AddMvc()
           .AddRazorOptions(options =>
            {
                options.PageViewLocationFormats.Add("/Pages/Shared/{0}.cshtml");
            });

The result was pretty disappointing. The only file that worked in there was _Layout.cshtml of the 4 I was hoping to put there (Error.cshtml, _ViewImports, _ViewStart). I'm sure other partial views would also work, but I was trying to declutter the root of my /Pages folder (so that it would only have Index.cshtml in it) but for that I was unsuccessful. The Error page route naturally only worked with /Shared/Error, which wasn't what I wanted. The _ViewImports/_ViewStart files simply stopped working.

Do you happen to know if there's a way to move the _ViewImports and _ViewStart files to a different, shared location?

What about putting razor page Error.cshtml somewhere else but still having its URL be /Error ? Thanks!

@pranavkm
Copy link
Contributor

Do you happen to know if there's a way to move the _ViewImports and _ViewStart files to a different, shared location?

Both these files are based on folder hieararchy. You could move it to a folder up (to the app root), but it has to be in the hierarchy of the file you're trying to execute.

The Error page route naturally only worked with /Shared/Error, which wasn't what I wanted.

You can use AddPageRoute (https://github.com/aspnet/Mvc/blob/dev/test/WebSites/RazorPagesWebSite/Startup.cs#L23) to create a route for your error page.

@hishamco
Copy link
Contributor

I faced similar issue two weeks ago when I was working on theming in Bloggy specifically when I created a custom IViewLocationExpander

It would be nice to add /Pages/Shared into the set of the locations here PageViewLocationExpander, IMHO /Views/Shared may useful for heterogeneous application (RazorPages & MVC)

@pranavkm
Copy link
Contributor

pranavkm commented Aug 1, 2017

Should be resolved as part of #6604

@pranavkm pranavkm closed this as completed Aug 1, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants