ASHX including code-behind class - - c#

I'm trying to use an external code file to include some helper classes for several .ashx handlers. The example one is using ListToJSON, which just turns nested Lists (Theres stuff like List<List<whatevers>> that are being worked with) into JSON (which will get thrown into context.response)
The ListToJSON class works fine when it's in the same file. I'm trying to put it into a Helper.cs file which is included in the same project in VS2010, because these classes get used in several different handlers.
I was under the impression that "using Helper;" was what I needed to do, but I still get the error "The type or namespace Helper could not be found (are you missing a directive or assembly reference?"
(and intellisense doesn't see it either)
I've also tried putting both code files into the same namespace. Same error.
It's not a DLL file, it's just a c# code file in the same project. Should I realy be compiling it as a DLL to do this? If so, how do I do that? (Once I do, I can do the right click->add reference, correct?)
I think I'm supposed to be using the App_Code folder, but I'm not really sure how to set that up in VS so that it's referenced properly.
My handler file (skeleton)
<%# WebHandler Language="C#" Class="generateReport" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Diagnostics;
using Helper;
public class generateReport : IHttpHandler {
public void ProcessRequest (HttpContext context) {
ListToJSON Converter = new ListToJSON();
//context.Response stuff goes here.
}
}
My helper file, Helper.cs - this is in the same project directory and is included in the project:
using <stuff>;
namespace Helper
{
public class ListToJSON
{
//class definition here
}
}
UPDATE: So, I put Helper.cs into the /App_Code/ folder, and it seemed to be playing nicely. Intellisense was picking up things in Helper.cs after I did using Helper;
When I tried it on IIS, it I got the following familiar error:
Compiler Error Message: CS0246: The type or namespace name 'Helper' could not be found (are you missing a using directive or an assembly reference?)
Line 19: using Helper;
Source File: <path>\info.ashx Line: 19
I get no errors when running this through visual studio's IIS emulator. When I run it through IIS (localhost) I get the IIS internal server error described.
App_Code folder was made through VS, I right clicked and chose Add ASP.NET Folder > App Code\
Edit: Added tag iis

I assume that you are using WebSite and not WebApplication.
While adding new Class file in the Website, It should be in the App_Code Folder to avoid the Accessibility issues.
Edit - 1 => Please see below the Publish details.
Edit = 2 => Try adding the individual Assembly in the bin folder as stated below. This will confirm that the App_Code folder Dll are incorporated.

For IIS to read classes correctly from the App_Code folder, the App_Code folder (or the bin folder, for compiled assemblies) needs to be in the IIS root directory in order for classes within to be detected.
In my case, I was publishing to a subfolder of the IIS root directory, and so IIS couldn't see the classes in it.

Related

Adding Global Using Directive in blazor

The type or namespace name 'XXX' does not exist in the namespace 'MyClassLibrary.Models' (are you missing an assembly reference?). The error refer to this BlazorProject\MyPager.azor.g.cs
I got this error after this scenario:
Created a file of type .cs inside my "Blazor project" and I added all my global directives by using keyword global inside this file.
Changed some namespaces in my "Class library"
Structure of the project:
Blazor-serve-side project using the class library
Class library.
When I go the page , I can't see any error, the error just in VS, I tried to clean the solution, rebuild, closing VS and reopen but it doesn't solve the problem. I am using VS22 and .Net6.
Note
I have also in my blazor project _Imports.razor file
#MisterMagoo, Thanks a lot.
what you said works fine, plus that I have to repeat the same namespaces with global directive file and also in _Imports.razor file, so the soultion:
In GlobalUsings.cs:
global using Microsoft.AspNetCore.Components;
global using Microsoft.AspNetCore.Components.Forms;
In _Imports.razor if you need the same namespaces in your razor pages, then you have to repeat them.
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
Keep in mind that the scope of "global using" isn't truly global. It's the current compilation, which typically means the current project (not the current solution). If you want to use this feature in two different projects in your solution (a Blazor Server project and a Class Library in your case, for example) you will need to create "global using" statements in both projects.

Compiler Error Message: CS0246 [duplicate]

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.

I am not able to add reference of my class into my aspx page

I am having a folder named "_CLASS"
in this folder i am having on class named "SqlAccess" which is having some methods.
now i want to use this class in my .aspx.cs page, so i have to add reference name space for the "SqlAccess" class, which is "SQL" (namespace)
but it give me error like
"The type or namespace name 'SQL' could not be found (are you missing a using directive or an assembly reference?)"
EDIT
This is my class which is under "_CLASS" folder
namespace SQl
{
public class SqlAccess
{
//...... here is my code
}
}
Or i need to add this class under "App_Code" folder ? if yes then why?
Or i need to add this class under "App_Code" folder ?
If you are using a WebSite then you should use the App_Code folder to add code.
If you are using a WebApplication, then you can place the code wherever you want.
Please refer to the WebSite vs Web Application article on MSDN.
Also from the error message you seem to have used SQL as namespace but in the code you have shown you used SQl which obviously is not the same thing in C#.
You can try this.
Right Click the class >> Properties and check the Build Action. It should be Compile.

asp.net cannot find class

I have some asp.net that I developed and is working just fine on a windows machine but now I'm trying to run it on a machine running Ubuntu and Mono 2.4.4 and ASP.NET 2.0 and getting errors.
I have some classes in my App_Code directory, such as DatabaseSingleton.cs, Functions.cs and NotAllowedException.cs. All of them are using the namespace aspapp.
Anyway, when trying to run a file (Login.aspx.cs) that uses the DatabaseSingleton class, I get that CS0103: The name 'DatabaseSingleton' does not exist in the current context. However, in a different file that isn't using DatabaseSingleton but is using NotAllowedException, I get a similar but a different error: CS0246: The type or namespace name 'NotAllowedException' could not be found. Are you missing a using directive or an assembly reference?. All the files are in the namespace aspapp and has using aspapp in the files.
Are there anything I need to add in the web.config to be able to access these files on mono?
Be sure you are pointing to mod-mono-server2 in your .conf file You can compile the .cs in one dll and move it to Bin directory

asp.NET 2.0 Web Site Can't Access Classes in App_Code

I'm having a problem accessing a class after deploying my website to a server.
Note that this is a Web Site not Web Application.The error is:
Compiler Error Message: CS0246: The type or namespace name 'Order' could not be found (are you missing a using directive or an assembly reference?)
Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433
I've done a lot of googling and haven't been able to find anything that helps.
My code looks like this (I get the compiler error on the line declaring Order):
//Default.aspx.cs
namespace Foo
{
public partial class _Default : System.Web.UI.Page
{
private Order custOrder;
....etc
}
}
In the App_Code folder I have Order.cs:
namespace Foo
{
public class Order
{
....etc
}
}
The strange thing is that this works fine locally and in a dev environment. When I move it to a qa environment, that's when I get that error.
I deploy by committing all the code to a subversion repository and then doing an export on the server machines.
I guess it could be some kind of namespace error, but I don't know why it would work on some machines and not others.
Any idea what could be different on a different server that might cause this? All machines report the .net version as 2.0
Right click the source file in App_Code and set its "Build Action" property to "Compile".
As it says, the error means that it couldn't find that type.
Possible causes:
A compilation error prevented the .cs files in your app_code folder from being compiled. When ASP.NET looks at the compiled code, it can't find the class you're looking for, thus the error.
The .cs files weren't deployed to app_code. Obviously if the source files aren't in the site, they can't be compiled.
A permissions issue is preventing the server from loading the .cs files.
One of your pages is incorrectly referencing the class - perhaps a typo in the .cs or the .aspx, a class moved to a new namespace or renamed, etc.
Essentially, the source couldn't be found, accessed or compiled.
A lot of times the bin directory is ignored in SVN (for good reason) so what ends up happening is that locally (when debugging) VS takes care of creating the App_Code.dll for you. What you will need to do is check to see if the bin directory is deployed to your server machines. If it isn't you have many ways to deploy your solution, by using deploy website, or ftp copy, etc.
Is the property Build Action set to Compile on your App_Code files ? It is not by default on Web Application Projects.
Are you deploying the .dll within your .aspx file?

Categories

Resources