App_Code folder issues - c#

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.

Related

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

Missing a using directive or an assembly reference error upon deploying the website

I have a website, where the *.cs file lies in the App_Code folder (while adding a class item in my project, VS2010 suggested me to create this folder). I have a default.aspx.cs file which makes use of this class. It runs without any error when I run on VS2010.
However, when I deployed the website on the webserver via a private hosting company. It gives me this error:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0246: The type or namespace name 'SMSAPP' could not be found (are you missing a using directive or an assembly reference?)
And its pointing to the line of code where I am instantiating the object of the class:
Line 84: SMSAPP obj = new SMSAPP();
What exactly went wrong from the local version to the hosted version? Please help. Thanks.
What I learned:
Okay, so I haven't been able to run my project yet. But I learned few things which I would like to share- web applications and web sites are totally different things. Mine was a website, that's why the *.cs code's property did not have option to "Build Action" = "Compile". I converted my project from website to web application, and I could see the options being suggested in the answers here. However, no luck in the deployment.
Right mouse click on the .cs file and select properties then check if the "Build action" is set to "Compile"
Check your local App_Code folder, somewhere there you must have a class SMSAPP.
You must have forgotten to upload this file.
Check property of your *.cs files in App_Code. They should have Build Action as Compile
Check in IIS manager if your web app is configured to use the correct .NET version

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.

Type or namespace could not be found from App_code folder

I have written a class called ArchivedFilesWrapper in the App_code folder of my project, however when I use this class in another file in a different folder i get error:
The type or namespace name 'ArchivedFilesWrapper' could not be found (are you missing a using directive or an assembly reference?)
I thought every page should be able to find classes that are contained within the same project, but I guess this is not the case. Can someone please tell me what using statement I need to have?
Here is a snippet from my class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EMCWebAdmin.App_Code
{
public class ArchivedFilesWrapper
{
Perhaps the problem will be solved by changing the Build Action Property of the *.cs source file to Compile from Content. From the Solution Explorer right click on the source file and choose Property.
Note that the App_Code folder is intended for use in Web Site Projects.
Note that for a Web Application Project or MVC project, adding an App_Code folder to your project and putting *.cs files in it will cause problems. I ignorantly added an App_Code folder to my MVC project from the Solution Explorer. VS defaulted the name space to MyProjectName.App_Code. In this case Visual Studio 2012 defaulted the Build Action to Content, even though the type was .cs code. After I changed Build Action Property of the *.cs source file to Compile from Content the namespace was resolved in other folder locations of the project. However because of problems, I had to change the name of folder--see below.
Important
In the MVC or Web Application project, the App_Code folder is trouble because it has Web Site Project type semantics. This folder is compiled when published (deployed) to the server. By changing Build Action from Content to Compile, you resolve the namespace issue on your development environment by forcing immediate compilation, but you get trouble when the second compilation results in objects defined twice errors on deployment. Put the code files in a folder with a different name. If you converted a Web Site to a Web Application, see the guidelines on the Net for this--not in the scope of this question. To read more about App_Code folder in the different project types see this blog
You need to add
using EMCWebAdmin.App_Code;
to all the pages you want to be able to use the class.
Alternatively you change the namesspace that the class is in to the same one that all the web pages use which presuming it is EMCWebAdmin
then in your class change
namespace EMCWebAdmin.App_Code
{
...
to
namespace EMCWebAdmin
{
...
This is a feature of visual studio, if you create a class in a folder structure, it uses a namespace that follows the folder structure.
If you convert it to a web app it should work. The downside is that it will no longer autobuild your code in app_code folder every time you change it and spin up the app. I have never seen a professional developer use a website project. I have no idea who MS were targeting when they created them.
Yes, put the calsses to another folder(not asp.net special folder), and only use the main namespace for the application is solve this issue.
Thanks johnmcp.

Compilation errors in Reference.cs after adding a Service Reference caused by multi-part namespace

I hit this weird namespace issue when adding my first 'Service Reference' to a client project in Visual Studio 2010.
If my project's default namespace uses two or more parts, e.g. MyCompany.MyApp then when adding a Service Reference a Reference.cs file is created containing the namespace MyCompany.MyApp.ServiceReferenceName with a lot of auto-gen code with fully qualified names, e.g. System.SerializableAttribute, System.Runtime.Serialization.DataContractAttribute.
The Reference.cs file will be full of compilation errors because the compiler starts treating the System namespace as sub member of the MyCompany.MyApp namespace. You get an awful lot of errors along the lines of:
The type or namespace name 'Runtime' does not exist in the namespace 'MyCompany.MyApp.System'...
If I amend the namespace at the top of the Reference.cs file to something simple, e.g. MyCompanyMyApp.ServiceRefernceName then the compiler behaves and recognises the System namespace references as decleration of .net's System namespace.
I'm using a different workaround for now as I really want to keep my multi-part namespaces. My current alternative is to append global:: in front of the System namespace references to force the complier to do the right thing. In fact, if the 'Add Service Reference' wizard uses T4 templates I may just amend those to embed my workaround at the source.
Questions
I'd really like to understand what's going on here and why a multi-part namespace causes this issue. Presumably there's more to namespaces than I thought. Secondly, would really like to work out a better solution than performing a global Find/Replace every time I add a Service Reference or mucking around with some T4 templates.
I found the answer here somewhat unclear, so I thought I would add this as an example (I would do it in the comments but it looks better here):
So I have this as my default namespace:
namespace RelatedData.Loader
But I also add a class named:
public class RelatedData
{
}
Because the class name matches a portion of the namespace when it generates your proxy with Add Service Reference it gets confused.
The answer here was to rename my class:
public class RelatedDataItem
Ahh well I found the cause eventually.
I'm working against a very large third party WCF API and ... one of their namespaces is LameCompany.System (!!) Carnage then ensues...
Arrrgghhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
The lesson to learn here is when Visual Studio/.net compiler stops recognising the BCL's System namespace you have a namespace/type in your project called System. Find it, remove it, shoot the developer that created it.
I found that having a class name similar to your namespace causes this.
Try renaming your class name
I ran into a similar issue with VS2012 described by jabu.hlong and Simon Needham after minor changes in the client project that has the references to the WCF services after updating the reference to the services. I got lots of errors compiling the Reference.cs files generated and so on (the generated files of the XAML as well).
I have selected to reuse types from specific assemblies in my solution and got a similar problems with the namespaces.
The error I get is that the namespace of the reused assembly and the namespace of the generated types can not be found when used in the Reference.cs. Both namespaces have in common the first parts, as they are from the same solution. My namespaces in the solution are like appname.tier.technology.project. Both conflicting namespaces are Appname.Dto.Modulename (the reused assembly) and Appname.Client.Wpf.ServiceName (the namespace in the client project using the services for the generated types).
The problem arises after a minor change in the client project, when I created a new utility class in the namespace Appname.Client.Wpf.Appname. I choose that namespace because the Appname is also the name of a module in the client project. This seems to confuse the compiler and can not resolve both namespaces in the generated Reference.cs. After changing the namespace of the utility class to avoid using two identical parts in it and updating the service reference, the compiler errors in Reference.cs dissapears.
I tried different things (and tried different namespaces when adding the service reference), but nothing worked for me except this brute force fix - in my case it was OK but I am aware it's ugly (and needs to be repeated if you use "Update Reference" in the future):
Since the WCF service namespace is added to your default namespace, just search and replace all mentions of the newly added
MyNamespace.ServiceNamespace
with
ServiceNamespace
in the whole solution (use your own namespaces of course), including the auto-generated Reference.cs file.
Basically, the problem is a name conflict where one name is hiding another. A folder or class named "System" can do that, but if you also have a class with the same name as your project, you'll see the same thing. Sure, you can rename everything in the reference.cs, but it's probably better to rename your conflicting class.
I had folder in my project called "System" (yes, very stupid of me) and that caused some issues in the references.cs.
Renaming the folder (and the namespace), fixed the issue.
Here is how I solve this issue on VisualStudio 2017 trying to add a reference to a web service in a test project.
After trying adding the references, rebuilding, closing, reopening and spending some time on the issue, I noticed that VS had put the files it creates to reference the WS in a folder named "Connected Services".
I renamed the folder without the space then opened all the files in the folder and the csproj with a text editor, replaced all the occurrences of "Connected Services" to "ConnectedServices" and reopened the project.
I then added references to System.Runtime.Serialization and System.ServiceModel and everything now works fine.
This is a bug in Visual Studio (still is at version 2022). To fix, remove the namespace in the reference.cs file. So if your namespace is "myapplication" and your service is "myservice", you'll see myapplication.myservice in the reference.cs file. just delete "myapplication." everywhere and make sure it isn't auto-generated again (lest you have to re-delete everything).

Categories