razor: Razor view can't locate corresponding page model

I upgraded to the latest v8 update (8.0.114) and the razor tooling is a little wonky.

# create project
dotnet new razor -o Demo

# repurpose default index page as "products" page
cd Demo/Pages
mkdir Products
mv Index.* Products

In Pages/Products/Index.cshtml.cs change namespace Demo.Pages; to namespace Demo.Pages.Products;

All that does is convert the “default” page to a custom “products” page, and puts that in a dedicated namespace. That is the typical setup, and used to work - every feature had it’s own directory and namespace, and the razor view would be able to identify the corresponding page model class, since it’s in the same directory.

Now for the bug. Open the razor page Pages/Products/Index.cshtml. The line @model IndexModel will be an error:

The type or namespace name ‘IndexModel’ could not be found (are you missing a using directive or an assembly reference?) [CS0246]

The workaround is to add @using Demo.Pages.Foo to every razor file.

Interestingly, the project compiles and runs without error, but our razor files are full of red squigglies.

Environment: .NET 8, VSCode, linux

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 22 (8 by maintainers)

Commits related to this issue

Most upvoted comments

@DustinCampbell I believe this is caused by https://github.com/dotnet/razor/commit/ac6e32a73fb93b3c699269fa432efac38fe4d5fe

Prior to that change, we emitted the #line directives for imports as fully qualified paths like all the other #line directives. After this change we emit them as relative paths

#nullable restore
- #line 1 "D:\Projects\scratch\razor_11712\Pages\_ViewImports.cshtml"
+ #line 1 "Pages\_ViewImports.cshtml"
using razor_11712;

That means that when we do this check: https://github.com/dotnet/razor/blob/ac6e32a73fb93b3c699269fa432efac38fe4d5fe/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/RazorCodeDocumentExtensions.cs?plain=1#L393 we decide they’re not in the same hierarchy and suppress the suffix. Thus, we omit the .Products on the namespace, which means we can’t find the namespace without an explicit using.

AFAIK This is a tooling-only path. The command line build continues to produce the expected #line directives and namespaces.

I’m looking at this now.

@lonix1 I created an insertion for VS Code at https://github.com/dotnet/vscode-csharp/pull/8336. Once that is merged, the fix will be in the next pre-release of the extension, and then flow to the release version after that.

It’ll require a new Visual Studio version but no SDK. It really is a tooling-only issue and the command-line compiler isn’t affected. We’re working on getting it into servicing for the most recent Visual Studio release.

It could be worse, you could be on Ubuntu. Since the bug is in the latest SDK update (rather than the C# extension for VSCode), we can’t even rollback because the apt package manager doesn’t offer older SDK versions, only the latest one. Can’t believe I actually miss Windows 😃.

Will this require another VS update, or is it something that can be patched out-of-band?

I believe this will need another VS update since it’s an IDE issue, not a compiler issue.

I no longer think this is a VS IDE issue. A coworker using Rider on a Mac can reproduce the problem with .NET 9.0.5 / SDK 9.0.300. Rolling back to SDK 9.0.204 resolves it. Should this be reported in a different repo?

I can effectively repro this issue, but oddly enough it only started happening after updating to Visual Studio 2022 17.14. Rolling back to the previous Visual Studio version fixes the issue. I have filed a bug report with a minimal repro https://developercommunity.visualstudio.com/t/Regression-in-VS-2022-1714:-Razor-edito/10904029

Essentially the namespace prefxing feature in _ViewImports.cshtml i.e. @namespace MyApp.Pages…is no longer working, so all razor views that are in sub-directories underneath /Pages fail to resolve their namespace scoped model unless the fully qualified namespace is used.