Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View components in Razor Pages #7018

Closed
ifo20 opened this issue Jun 13, 2018 — with docs.microsoft.com · 4 comments
Closed

View components in Razor Pages #7018

ifo20 opened this issue Jun 13, 2018 — with docs.microsoft.com · 4 comments
Assignees
Labels
Pri1 Source - Docs.ms Docs Customer feedback via GitHub Issue

Comments

Copy link

ifo20 commented Jun 13, 2018

I'm confused with this instruction: "Create the Views/Shared/Components folder. This folder must be named Components."
I am using Razor Pages rather than MVC. I don't have a Views folder or a Shared folder (infact the Razor intro says I shouldn't have a Views/Shared folder).

In my basic app I currently have, at the root level, a Pages folder, and a ViewComponents folder.
Should I create this 'Components' folder at the same level? Or within the Pages folder? Or can I put the same files in the ViewComponents folder?


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@guardrex
Copy link
Collaborator

guardrex commented Jun 13, 2018

Hello @ifo20 -- Thanks for asking. This topic is due for a 2.1 update that will convert it over to Razor Pages. The work is tracked on #5495.

The engineers did provision Razor Pages apps to have a Shared folder on aspnet/Mvc#6604. You can add a Shared folder to your Pages folder. I think* that either of these paths will be ok ...

  • Pages/<controller_name>/Components/<view_component_name>/<view_name>
  • Pages/Shared/Components/<view_component_name>/<view_name>

*think = I haven't tested personally.

RE: The topic update, I'll attach this issue to the sample update tracking issue. I don't think we want to patch just that one spot for Razor Pages. The topic and sample are currently geared to MVC. The topic has a awful lot of "view" language that will require attention.

@guardrex guardrex added Pri2 Source - Docs.ms Docs Customer feedback via GitHub Issue question labels Jun 13, 2018
@guardrex guardrex added this to the Backlog milestone Jun 13, 2018
@Rick-Anderson
Copy link
Contributor

@ifo20 if you create a Razor Pages project with the 2.1 templates, it has Pages/Shared

@ifo20
Copy link
Author

ifo20 commented Jun 30, 2018

Ah thanks guys - I was using 2.0 hence the template did not create the Shared folder. I am now using 2.1 and created a Components folder within the Shared folder as suggested.
The below error message suggests I have three options for where I should be placing the view component 'Default.cshtml'. To me it seems the first two are consistent with the approach suggested by @guardrex , whereas I think the third option is not appropriate for Razor pages (as a Razor Pages web app isn't supposed to have a Views folder ... right? I am a beginner in this so would appreciate any confirmation)

InvalidOperationException: The view 'Components/Nav/Default' was not found. The following locations were searched: /Pages/Components/Nav/Default.cshtml /Pages/Shared/Components/Nav/Default.cshtml /Views/Shared/Components/Nav/Default.cshtml

@guardrex
Copy link
Collaborator

a Razor Pages web app isn't supposed to have a Views folder

An app can have both. Razor Pages works with MVC as long as there are no route conflicts between controllers and pages.

Sharing a layout among views and pages is a bit of an open question right now. A layout can be shared when full paths to it are used, but there's no "best practices" guidance from engineering yet on where to keep the layout page in the app. See the convo at #7336 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pri1 Source - Docs.ms Docs Customer feedback via GitHub Issue
Projects
None yet
Development

No branches or pull requests

4 participants