Reputation: 375
I am trying to work through this tutorial, ASP.Net Core Razor Pages, but (often) when I build the solution, I get a CS0234
error stating that a namespace is missing from file Index.g.cshtml.cd
, but where does this file exist?
I have tried
Surely there must be a fix for this? Can you assist?
Upvotes: 30
Views: 23957
Reputation: 21
May be my answer is something late, but hopefully it will help someone: I have faced the dame issue because I have changed the Project name and later I tried to change the namespaces inside controllers and models, then I found out there is a direct mentioning of the namespace inside (Pages/_ViewImports.cshtml) from there I updated them to match the namespace used inside my models and the issues gone. ViewImportsImage
Now my ViewImports.cshtml content looks as follows: ViewsImport.cshtml content
Upvotes: 2
Reputation: 135
Add full namespace in _ViewImports
@using Microsoft.AspNetCore.Http
Upvotes: 0
Reputation: 351
If your solutions keeps failing because of those .g .cs files, it could be because you forgot to update the namespace of a class that you may have moved, in the actual cshtml file (example via an injection of the said namespace).
This is what solved the issue for me: https://til.secretgeek.net/.net_core_MVC/error_in_g_cs.html
Upvotes: 2
Reputation: 21
It's Microsoft.AspNetCore.Razor.Design Package issue.
I tried with 2.2 version in my VS2017 which does not support this and gives ".g.cshtml not found" in obj directory.
I solved issue as below:
I downgraded ASP.NET Core version of my project 2.2 to 2.1
Changes done in startup.cs:
and issue resolved.
Upvotes: 0
Reputation: 51
Recently I have faced the same issue,
Try the following simple method
copy the error and paste it in the notepad. There you can able to see the original root cause path of the error file, e.g 'YourProjectFolder'\obj\Debug\netcoreapp2.2\Razor\Views\'View Name'\index.g.cshtml.cs
Locate the file, if the error really persists refer the correct namespace in your index.cshtml file else delete the file and rebuild.
Also check you have any duplicate file and which was excluded from the project. If it is exists delete the file too
Now clean and rebuild the project. (You may need to clean, close VS, rebuild)
Upvotes: 3
Reputation: 161
I landed here with the same errors with Visual Studio 2019 16.2.2. For me, I had changed the name of some of the models and rather than picking up the error in the actual views which were using the models, the error showed in page.g.cshtml rather than page.cshtml.
So, my solution was rather simple. If the error was in page.g.cshtml, ignore the g, go to the view page.cshtml, update the model name to the correct name, rebuild and errors gone.
Upvotes: 15
Reputation: 23937
If an error says a using directive or namespace is missing from xxx.g.cshtml.cs
, that usually means, that there is a directive directly in the view (.cshtml
or .vbhtml
), that the compiler cannot resolve.
In my case I had the following statement in a partial view:
@inject UserManager<AppUser> UserManager
I then went along and refactored the AppUser class from the web application Namespace.Web.AppUser
into Namespace.Entities.AppUser
, which then was causing that error.
There are different ways to resolve this. The easiest way is to use a fully qualified type namespace:
@inject UserManager<Namespace.Entities.AppUser> UserManager
And do not forget to add a reference to the other project, if it is in another project. Or: add a using statement:
@using Namespace.Entities;
Or: Add the using statement above to your ~\Views\_ViewStart.cshtml
. This will make that class available in all views.
Upvotes: 42
Reputation: 4119
Try to clean all the obj folders in all of your projects and build again. This is part of the code that is generated by the compiler, sometimes it is not synchronized with the view itself, even Rebuild that should clean everything doesn't work. Do it manually. Hope this helps
Upvotes: 0
Reputation: 59
right click your project and open folder in Explorer. Then search for "Index.g.cshtml.cs" Then delete the file search results
Make sure your Index.cshtml is referencing the correct model.
Upvotes: 5
Reputation: 375
There is actually no real answer to this question. It turns out that the file is create inside the DEBUG folder. However, after deleting the Debug folder, I was still getting the error. So it must be a VS bug?
Upvotes: 0
Reputation: 300
you can try below solution
Upvotes: 0