Skip to content

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
mainfrom
dev/hmckelvie/fix-2989432
Open

fixes Navigating to /Blogs throws InvalidOperationException due to view folder mismatch after MVC scaffolding with EF#3748
haileymck wants to merge 2 commits into
mainfrom
dev/hmckelvie/fix-2989432

Conversation

@haileymck

Copy link
Copy Markdown
Member

… folder mismatch after MVC scaffolding with EF

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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., BlogsControllerViews/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants