Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is index.g.cshtml

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

  1. All the build/clean/rebuild solution options.
  2. I have restarted Visual Studio
  3. I have deleted the DEBUG files and restarted VS and FINALLY
  4. I have restarted my PC. The ONLY solution that seems to work, is to delete the entire solution/project and start again.

Surely there must be a fix for this? Can you assist?

like image 366
ImConfused Avatar asked Sep 23 '18 04:09

ImConfused


4 Answers

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.

like image 51
Marco Avatar answered Oct 06 '22 10:10

Marco


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.

like image 45
96marrs Avatar answered Oct 06 '22 10:10

96marrs


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.

like image 5
Botlhale Jonathan Matlou Avatar answered Oct 06 '22 09:10

Botlhale Jonathan Matlou


Recently I have faced the same issue,

Try the following simple method

  1. 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

  2. Locate the file, if the error really persists refer the correct namespace in your index.cshtml file else delete the file and rebuild.

  3. Also check you have any duplicate file and which was excluded from the project. If it is exists delete the file too

  4. Now clean and rebuild the project. (You may need to clean, close VS, rebuild)

like image 3
Magesh Avatar answered Oct 06 '22 09:10

Magesh