Where is index.g.cshtml - c#

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
All the build/clean/rebuild solution options.
I have restarted Visual Studio
I have deleted the DEBUG files and restarted VS and FINALLY
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?

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.

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.

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.

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)

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

you can try below solution
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
Try to delete those files and rebuild your solution that might help you

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?

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

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:
CompatibilityVersion.Version_2_2 to CompatibilityVersion.Version_2_1
Change all dll of Nuget packages to 2_1 supportive dll
SDK make sure its 2.1.x
and issue resolved.

Add full namespace in _ViewImports
#using Microsoft.AspNetCore.Http

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

Related

ASP.net core namespaces out of sync

I decided to change the name of a folder within "Models->ABC->A" to "Models->ABC->B" Manually.
the problem is, now in the view imports i'm getting old suggestions A but not B. i don't understand where the A is coming from? i cleaned the solution, rebuilt the solution. checked in directory but A doesn't exists there still it shows A in suggestion. it seems when i build the project Visual Studio (2019 Community) is using the same old data and namespaces are out of sync. any idea how to fix it?
The namespace given in the Class is having the old namespace that is Models->ABC->A. Change it to Models->ABC->B.

Schema specified is not valid. Errors: Multiple types with the name

I am working in EF 6 and facing the following issue when I try run my project.
Schema specified is not valid. Errors:
Multiple types with the name 'TableName' exist in the
EdmItemCollection in different namespaces. Convention based mapping
requires unique names without regard to namespace in the
EdmItemCollection.
I tried too much on StackOverFlow and google and found no solution. I am using visual studio 2012. I crated a single .edmx file for whole of my database. I searched the tableName throughout the project and there is single occurrence of table.
Please help me in this regard
Finally I figured out the solution of the problem.
In my question above, I stated that I have only single edmx file in my project. It is true in one sense. But wrong in the other sense.
I am using git to manage my code. I created another edmx file in another project and used that project's reference in my main project.
Then I switched git branch with dll in .gitignore. Those dlls which were in .gitignore could not be replaced by git with the newly switched branch's dlls.
So I had two references of edmx files in my project with fully built dlls.
Problem solved when I keenly checked my bin folder and removed the dll which was actually not belonging to the current git branch.
I hope it will help you.
Comment here if some thing is not clear.
Thanks
I spend hours fixing it, tried publishing/deploying all the projects but no luck. So, finally found out that there was a dll that was referenced twice. But with slightly change in name but pretty much similar content. So, program wasn't sure which file to look at. I redeployed all the projects but still no luck. So, finally deleted all the files from server except webconfig and global asax. And the deployed the files again. Finally it worked.

' Sequence contains no elements' exception when initializing ninject

I have a problem with Ninject which I have not seen before and don't know how to fix. I am using the MVC NuGet package for MVC 5.
In my NinjectWebCommon.cs, there is the following method:
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
However, it falls over when trying to execute the last line, complaining that the "sequence contains no elements". I was previously using Unity for DI but have changed to Ninject.
Any ideas?
Thanks in advance, M
In my case, my project had two NinjectWebCommon.cs folders with the same/similar content.
Dropping the folder has resolved the issue.
I faced this type of error while preparing a new solution by replacing File Name and other text in Entire solution.
Everything was perfect like buld the application.
But when i go to run the application it says me "Sequence contains no elements" error.
And finally i finally resolve that issue by using following.....
open every project folder by right click on the project >> Then select "Open Folder in File Explorer
Remove 'bin' and 'obj' folder
Clean Entire project
Build application
and finally run the application.
Hopefully, it will work for you....
Thanks...
For my two penneth and related to #Pavan's post. I started receiving the error after renaming an assembly. I had the old DLL lingering in the bin folder. Cleaning the solution didn't fix it. I had to go in and delete the contents of bin by hand.
I think this is a dupe of this question: Ninject + MVC3 = InvalidOperationException: Sequence contains no elements
TL;DR - make sure you clear out all old dlls when you publish your site.
An additional explanation detail to extend the helpful answer by #Greg_B:
A reference to another project in the solution that also uses Ninject. Removing this reference solves the problem.

The type or namespace name GridViewHelper could not be found

Fist off, I'm very new to ASP.NET and Visual Web Developer Express.
I have a datagrid that I want to display subtotals in and have grouping.
I found this http://www.agrinei.com/gridviewhelper/gridviewhelper_en.htm which looks great. I've added the classes files to my projects App_code folder and re-built the project.
But when I try and use the GridViewHelper I get the error "The type or namespace name GridViewHelper could not be found"
Tried adding using GridViewHelper; to the top of my page but that also gets underlined and the same error.
So how do I tell my project to use the classes I've added?? I think this is a really stupid question but I cant find an answer!
I downloaded the source code from the link you provided, I believe the reason you can't add it is because it is not an actual namespace.
However, in the app_code, they provided a GridViewHelper.cs, just make sure it's in your own app_code file and it should work.
Found the answer here: App_Code folder is missing in VS 2010
So, for Web App Projects, you should instead Add a folder called something like 'CodeFolder' and then add you class in there. Then right click properties on that class file and set its build config to compile rather than content.
It's now working fine.

App_Code folder issues

So I'm having a really weird issue with my App_Code folder on a new website I'm designing.
I have a basic class inside of a namespace in the App_Code folder. Everything works fine in the IDE when I setup the namespace and make an object from the class. It brings up the class summary on hover, and when you click on "go to deffinition" it goes to the class file.
And it also works fine localy.
However, when I load the site onto my server, I get this error message when I access that page:
Line 10: using System.Web.UI.WebControls;
Line 11: using System.Web.UI.WebControls.WebParts;
Line 12: using xxxx.xxxx
Compiler Error Message: CS0246: The type or namespace name 'xxxxxx' could not be found (are you missing a using directive or an assembly reference?)
I know for a fact that the class file is there. Anyone have any idea of whats going on?
Edits:
John, yes it is a 2.0 site.
The problem that your classes are not compiled, You'll solve this issue simply by going to the properties of any class in the App_Code folder and change it's 'Build Action' property from "Content" to "Compile"
If your application is a Web Application project rather than a Web Site project, the code files should not be in the App_Code folder (stupid design, I know). Create a new folder called code or something and put them in there.
It caused me all sorts of problems when I upgraded a bunch of old .Net web sites to application projects.
This just happened to me and the solution was that App_Code (and App_Data) were not put in the root of the server, but in a subfolder that held everything else. Must be in root!
I have noticed a mismatch sometimes between the IDE parser and the compiler whenever a compile-time error occurs in a referenced assembly or code file. In that circumstance the IDE will correctly identify the types and provide full support for them, but since the compiler was unable to create the referenced objects, it will complain that the referenced objects don't exist.
Now I don't want to go accusing anybody of anything—this is just a guess—but you should probably make sure there are not any errors in your referenced code file.
Depending on how you publish the site, it won't look in App_Code, it'll look for a DLL in the Bin folder that contains the class instead. How did you transfer your website to the server?
For those that follow...I had this same set of issues but it was caused because I named a class in App_Code, 'HTML'. Took a long while to figure out that it was just a name conflict because the compiler wasn't being very helpful about telling me what the problem was.

Categories