fixes Navigating to /Blogs throws InvalidOperationException due to view folder mismatch after MVC scaffolding with EF#3748
Open
haileymck wants to merge 2 commits into
Open
fixes Navigating to /Blogs throws InvalidOperationException due to view folder mismatch after MVC scaffolding with EF#3748haileymck wants to merge 2 commits into
haileymck wants to merge 2 commits into
Conversation
… folder mismatch after MVC scaffolding with EF
438ec11 to
970c48c
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a runtime InvalidOperationException when navigating to an MVC EF-scaffolded controller route (e.g., /Blogs) caused by generated Razor views being placed in a folder that doesn’t match MVC’s view discovery conventions.
Changes:
- Extend
ViewHelper.GetTextTemplatingProperties(...)to optionally override the view output folder name (defaulting to the model type name). - Update the EF MVC views scaffolding step to derive the views folder from the controller root name (e.g.,
BlogsController→Views/Blogs/).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/dotnet-scaffolding/dotnet-scaffold/AspNet/Helpers/ViewHelper.cs |
Adds an optional viewFolderName parameter to control where view files are generated. |
src/dotnet-scaffolding/dotnet-scaffold/AspNet/Extensions/EfControllerScaffolderBuilderExtensions.cs |
Uses the controller root name to align generated view folder paths with MVC conventions. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+187
to
+191
| var controllerName = efControllerModel.ControllerName; | ||
| const string controllerSuffix = "Controller"; | ||
| viewFolderName = controllerName.EndsWith(controllerSuffix, StringComparison.Ordinal) | ||
| ? controllerName.Substring(0, controllerName.Length - controllerSuffix.Length) | ||
| : controllerName; |
Comment on lines
+45
to
46
| internal static IEnumerable<TextTemplatingProperty> GetTextTemplatingProperties(IEnumerable<string> allT4TemplatePaths, ViewModel viewModel, string? viewFolderName = null) | ||
| { |
- Validate viewFolderName against path traversal (reject '.', '..', and names containing invalid path chars) before using it as a Views subfolder, falling back to ModelTypeName on rejection - Add unit tests: viewFolderName override uses the provided name, null falls back to ModelTypeName, invalid names (path traversal attempts) fall back to ModelTypeName
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2989432