Search code examples
c#asp.netrazorscaffoldingasp.net-core-2.1

Scaffolding fails because RazorPageWithContextTemplateModel doesn't contain a definition for ViewName?


I have an ASP.NET Core Razor Pages project that was working fine with scaffolding until I upgraded to .NET Core 2.1. Now, when I try to scaffold, I get the following error:

'Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Razor.RazorPageWithContextTemplateModel' does not contain a definition for 'ViewName'
   at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine>b__6_0()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args)
   at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)

Google doesn't give me much info, and the error doesn't even point to a specific file in my project. I searched all instances of ViewName in the templates, and it's only used in the context of @Model.ViewName. Not really sure where to look from here. Any ideas?

Maybe my templates are out of date? But then, where can I get new ones?


Solution

  • Okay, so it turns out there is no ViewName for Razor Page Templates in 2.1.

    You need to do a Find & Replace on all files in the \Templates\RazorPageGenerator\ folder from "Model.ViewName" to "Model.RazorPageName".

    After that, my scaffolding worked great. But I do wish the initial error pointed me to the actual problem a little better.