Where is index.g.cshtml

2020-07-03 03:45发布

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?

8条回答
2楼-- · 2020-07-03 03:52

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.

查看更多
放我归山
3楼-- · 2020-07-03 03:53

you can try below solution

  1. Go to C:\Temp\msbuild 2.There you would find folder with your project name and inside those folder you find these files as shown in below image
  2. Try to delete those files and rebuild your solution that might help you
查看更多
相关推荐>>
4楼-- · 2020-07-03 03:54

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.

查看更多
女痞
5楼-- · 2020-07-03 03:54

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?

查看更多
萌系小妹纸
6楼-- · 2020-07-03 03:55

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.

查看更多
我欲成王,谁敢阻挡
7楼-- · 2020-07-03 03:58

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:

    1. CompatibilityVersion.Version_2_2 to CompatibilityVersion.Version_2_1
    2. Change all dll of Nuget packages to 2_1 supportive dll
    3. SDK make sure its 2.1.x

and issue resolved.

查看更多
登录 后发表回答