Can't see an added class - asp.net

I created an ASP.Net project in .Net 2.0 and uploaded all files to the server (dlls and cs files) to the server. It is a very basic HTML page with a button on it.
When I loaded it on the server, it was okay.
I added a static class to my project and used it when the button was clicked:
protected void btnUse_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtData.Text))
Utils.SaveData(txtData.Text);
}
When I uploaded it, I get the following error:
Server Error in '/' Application. 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: CS0103: The name 'Utils' does not exist in the
current context
What else should I upload?

The code change can't take effect until the code is compiled, and the result is the .dll file(s). So two things to take care of here:
In order to add the class, you need to add it to the source code (which you've done), re-compile into the .dlls, and upload the new .dlls to the server.
You don't need (and shouldn't have) the .cs files on the server. That's the source code, not the compiled result. The compiled result is the .dlls. The server needs those, the markup pages (.aspx, .ascx, etc.), and any other resources (images, JavaScript files, .css files, etc.), but not the C# source code.

Related

global.asax does not work

I am developing an asp.net web application and i have a problem with Global.asax file,
when i publish my website and upload it to server the Global.asax is missed, it will be gone to bin folder and Reshape to Dll file.
but it does not work!
and when i uploaded the Global.asax file to the root folder it works but i have an other error like this
Compiler Error Message: CS0433: The type 'ASP.global_asax' exists in both 'c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\93c82a26\eca26fde\assembly\dl3\b8f9aab8\18e0a0de_5afccf01\App_global.asax.DLL' and 'c:\Windows\Microsoft.NET\Framework64\v2.0.50727\Temporary ASP.NET Files\root\93c82a26\eca26fde\App_global.asax.o53z7epf.dll'
protected ASP.global_asax ApplicationInstance {
get {
return ((ASP.global_asax)(this.Context.ApplicationInstance));
and its not possible to clear the temp folder because there will be on other error, please help me.
The problem occurred in a virtual application's bin folder. When you look into this bin-folder you will probably see two files (an information I found here):
App_global.asax.dll
App_global.asax.compiled
Removing these resolves the error. The App_global.asax.dll is generated at runtime too which causes the problem.

Modifying ASPX.CS file has no effect

One of my colleagues maintains a legacy ASP.NET site. Today she bumped into a strange problem:
There is an ASPX page, which throws an exception.
If she deletes the line of the ASPX.CS file which throws the exception, the exception remains (with the same call stack). I'm sure she modified the very same file the calls stack refers to.
If she renames the ASPX.CS file, the server says the file is missing.
If she restarts the server after modifying the ASPX.CS, the exception remains.
If she modifies other ASPX.CS files, she gets the expected effect.
She doesn't compile the page into DLL.
AFAIK IIS should recompile the file when its content changes (MSDN), but this mechanism doesn't work in this case. What should we do? Thanks for your help in advance.
Check to see if the website is a website or web application. Web applications compile all the code behind into DLLs and place them into the 'bin' folder. In order to make changes to the code available, you would have to 'build' the application in VS.
If it is not a compiled web application, then she should check to see if the front end page is pointing to the correct code behind page. Sometimes if you manually copy and rename files, this piece could be left pointing to the original page, not the new .cs file.

Page class outside of App_Code will not compile

I have a website that has 2 files as follows:
page.aspx
page.aspx.cs
It used to be that I could just drop new files onto the web server and IIS would automatically compile the files and I could access the page e.g.
http://www.website.com/page.aspx
... and the associated functionality in the page class contained in the .cs file would work nicely.
Now I get the error: "Could not load type namespace.classname" which refers to my page class.
Now for some strange reason I have to put all my .cs files, even page classes into the app_code folder.
All that has changed on my website is that I reorganised the structure so that instead of my pages being on the web root they are now inside http://.../newfolder/page.aspx.
For some reason all my page.aspx.cs files now have to be in app_code.
Any ideas?
Sounds like you are mixing up a Web Application Project and a Web Site.
Are you sure the files are exactly the same? Perhaps one #Page directive says CodeBehind=Page.aspx.cs and the other says CodeFile=Page.aspx.cs?
CodeBehind requires project compilation, so you cannot just drop in a new .cs file, you need to upload a new compiled DLL. CodeFile will allow dynamic compilation.
The App_Code directory is dynamically compiled (in both cases) when your app is accessed, so the Inherit directive has a valid type when you put the file there. In general, don't do this. You want the .cs file to go with the .aspx file. Use App_Code for business logic or utility classes that aren't associated with a particular page.
Finally, is this new subdirectory set up as a new app in IIS? What does the web.config file in your new directory change? Are you running the same version of ASP.NET? Check the "compilation" tag. I'm not sure what you could do there to cause this, but I'm sure you could cause some chaos.

Reasons why WebResources cannot be found

Hi I am having trouble getting an embedded js file to work.
I have tried all of the following:
Running Cassini development server (VS2008, .NET 3.5)
Added [assembly: WebResource("MyNamespace.MyScriptFile.js", "text/javascript")] above the class's namespace declaration.
Script file has build action "Embedded Resource".
Tried registering during OnInit, OnLoad and OnPreRender
Script file is in the same assembly and namespace as the control registering it.
Opened assembly with Reflector and verified the name of the resource is correct.
Does not work using any of the following methods:
ScriptManager.RegisterClientScriptResource(Page, GetType(), "MyNamespace.MyScriptFile.js");
Page.ClientScript.RegisterClientScriptResource(GetType(), "MyNamespace.MyScriptFile.js");
Page.ClientScript.RegisterClientScriptInclude(GetType(), "key",
Page.ClientScript.GetWebResourceUrl(GetType(), "MyNamespace.MyScriptFile.js"));
Other WebResource.axd files are being found - only this one is not being found.
The request for the resource returns a 404 page with an exception listed: "*[HttpException]: This is an invalid webresource request.*"
Using the ScriptManager.RegisterClientScriptResource produces the exception:
"*Web resource 'MyNamespace.MyScriptFile.js' was not found.*"
In your example code, you are making a call to GetType()... the type is used as the starting point for the search. Depending on where you are making your call to GetType(), you may not be getting back what you expect. Since ASP.NET dynamically compiles types for your pages and custom controls, GetType() may be returning a type defined in a new assembly built by ASP.NET.
You could try doing typeof(SomeType) instead, where SomeType is appropriate based on the location of your resource (ex. the control type you're working with).
maybe your resource file is located inside folder(s) in project?
if so, you should specify another name/location string in assembly and when you register the script
Page.ClientScript.RegisterClientScriptResource(GetType(), "MyNamespace.Folder1.Folder2.MyScriptFile.js");
[assembly: WebResource("MyNamespace.Folder1.Folder2.MyScriptFile.js", "text/javascript")]
usually that's a common problem
Try implementing your own ScriptManger and then adding the embedded file from there. Here's an example
public class MyScriptManager : System.Web.UI.ScriptManager
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
RegisterClientScriptResource(this, typeof(ScriptManagerExtension), "MyNamespace.MyScriptFile.js");
}
}
An alternate cause of this issue - you've been too heavy handed with your Global.asax, and you've said that everything else after your rules gives you the homepage. Not so clever of me, an hour or two wasted on that!

Custom Errors for "App" folders? (ASP.NET)

Where can I setup custom errors for directories in my application such as App_Code, App_Browsers, etc.? I already have customErrors configured in the web.config and that works as expected. For example,
http://www.mysite.com/bla.aspx > redirects to 404 page
but
http://www.mysite.com/App_Code/ > displays "The system cannot find the file specified."
There's no physical App_Code directory for my site. Is this something that I can change in IIS?
You are trying to server content from an Protected Folder... ??
I think you might need to allow access to these folders to get the nice errors you are looking for...
http://www.webdavsystem.com/server/documentation/hosting_iis_asp_net/protected_folders
That being said... there is a reason these folders are protected.
I would never put anything i needed IIS to serve in protected folders.
But there are always reasons to do do something? i have broke a few rules in my short lifespan :)
UPDATE:
Found this when i tried this locally: http://support.microsoft.com/kb/942047/
Looks like those reserved directories throw special 404's you might be able to get IIS to Target the 404.8 type... with out opening up serving to those directories
I believe you will need to set the error pages in IIS itself, as the requests you talk about never reach the ASP.NET application. The reason your first example works is because IIS recognises the .ASPX extension and forwards it to ASP.NET.
One way is to provide a redirect in the global.asax file:
void Application_Error(object sender, EventArgs e)
{
//uncomment this to narrow down 'helpful' microsoft messages
//HttpRequest request = ((HttpApplication)sender).Context.Request;
Exception ex = Server.GetLastError();
//ErrorManager is a custom error handling module
ErrorManager.ProcessError(ex);
Response.Redirect("~/error.aspx?error=" + HttpUtility.UrlEncode(ex.Message), true);
}
{ On a side note, I was getting an exception that I just couldn't track down - it just said 'file not found' but didn't say which file was missing. It turned out to be a broken image reference in a css file - breaking on line two of the code above helped identify the missing file }
Add a Wildcard Mapping to IIS to run ALL Requests through ASP.net, then you can use Global.asax to handle the error.
Taken from here:
Follow these steps to create a wildcard script map with IIS 6.0:
Right-click a website and select Properties
Select the Home Directory tab
Click the Configuration button
Select the Mappings tab
Click the Insert button (see Figure 4)
Paste the path to the aspnet_isapi.dll into the Executable field (you can copy this path from the script map for .aspx files)
Uncheck the checkbox labeled Verify that file exists
Click the OK button

Resources