I've got a website that I published on IIS, but it seems like I've got the wrong version of source code in my dev environment.
I just need the code from 1 ascx.cs file.
Can I get to the contents of an ascx.cs file from a deployed website?
I've tried using .NET Reflector as suggested on other posts, but it seems like this tool only gives me access to my normal classes and not to the .ascx.cs files. Or am I just missing that?
EDIT:
Additional information as requested in answers.
<%# Control Language="C#" AutoEventWireup="true" CodeFile="timesheet.ascx.cs" Inherits="controls_timesheet" %>
You should use the DLL of the compiled web site to get the class that represents your .ascx.cs file.
For example, if your project is called MyWebSiteProject, you should have a MyWebSiteProject.dll on your /bin folder.
Open this DLL with .net Reflector, or http://ilspy.net/, if your control is named MyUserControl, it should be inside the namespace MyWebSiteProject.
Please double check your project's and controls namespace first.
Hope this helps, good luck :)
Related
I am considering to do a new project in .NET.
It is maintenance of existing project. I do not have much experience with .NET projects, just some.
Client sent me source code (at least he told me that this is source code), however, I do not see any *.cs files there. It has some dll files and ascx files.
For example, default.aspx file has only one code line
<%# Page CodeBehind="default.aspx.cs" Inherits="Tere.Web._default" %>
Master page contains some view code (and not Code behind code). The first line of master page is the following:
<%# Master CodeBehind="Site.master.cs" Inherits="Tere.Web.Master.SiteMaster" %>
This is a bit confusing for me at this point.
And there is a dll called Tere.dll and Tere.Web.dll.
Can you eplain me, please, does this means that client gave me already compiled website without source code? Or maybe he has not sent me the whole project? So, what should I tell him - that this is not the source code as he thinks?
The source code would contain (at least) two files for each webpage, for example default.aspx and default.aspx.cs
When this is compiled, all the .aspx.cs files are compiled into a DLL, while the .aspx files remain as they are. This is what you've been given. You'll probably be able to make certain purely-presentational changes with this (the .aspx file contains the HTML), depending on how exactly the pages were designed. If you want to change what the site does though, you'll be out of luck - the behaviour is coded up in the .aspx.cs files which you haven't been given.
Yes looks like he's just given you the compiled code and not the code behind files that are linked to the actual pages.
Tell him that he's just supplied the files for deployment rather than the actual source.
From your description I suspect you're right: It is not the actual source. The client probably gave you the compiled code that's been deployed to the webserver.
Your client did simply not sent you the source code, but instead the compiled code (dll) and additional files for the website (aspx). The files with the source code are .vb or .cs, as you know it.
your c# or vb codes are compiled and stored in you dll files and there is no way to decompile them. When I do a project for a customer, I always send the web.config, dll files, .aspx files and all the util files.
Assume there is a asp.net 4.0 web application and it has a default.aspx and default.aspx.cs files in it. After I build the project, a dll that is named of the project created in the bin folder. So what the dll contains ? All code behind files compiled versions ?
If the aspx files still refers its CodeBehind file like below, then does the dll used for this aspx file or still code behind is valid to run the project ?
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>
In a Web Application, all your C# code is contained within the DLLs in the bin directory. There are a couple of exceptions, such as DLLs that you rely on that live in the GAC, for example. Using a web application ( your question says this is what you are using ), you do not need to deploy your *.cs code behind files.
A Web Site is different. Changes are detected and recompiled on the fly. You'll need to include your C# files code-behind files when creating a Web Site type project.
ASP.NET Web Site or ASP.NET Web Application?
A DLL is a library that contains code and data that can be used by more than one program at the same time:
What is a DLL?
Code-behind refers to code for your ASP.NET page that is contained within a separate class file. This allows a clean separation of your HTML from your presentation logic:
ASP.NET Code-Behind Model Overview
The code gets compiled into the assembly produced by your web project. You can change its name and default namespace as part of project options.
As I understood it, the ASPX file is only used IF the project is marked as being updatable, otherwise it is just a placeholder file.
I have a Visual Studio 2010 project with a Master Page that works fine.
~/Site.Master
In the same project is a Folder with another directory structure that needs to use another Master Page.
/xr
Within this folder, I have included the Master Page for those files.
/xr/XR.Master
My Default.aspx file attempts to access this Master Page using the following include:
<%# Page Title="XR Project" Language="C#" MasterPageFile="~/xr/XR.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Test1.Default" %>
Debugging on my machine in localhost works fine, but whenever I deploy it and try to navigate to the Default page in the xr folder, I get
Parser Error Message: The file '/xr/xr/XR.Master' does not exist.
However, the Source Error returns Line 1 as I have shown above.
Here is what I tried:
I moved the XR.Master page into the root folder and changed all of the files in the xr folder to reference the XR Master Page using the following include:
I debugged and tested the code on my machine using localhost, and it worked fine.
I pushed it out to the remote website and got the new error:
Parser Error Message: The file '/xr/~/XR.Master' does not exist.
What am I doing wrong?
EDIT: I do not like posting a link to the page, but I will for a short time until this is resolved:
Projects
From there, you will see everything works well enough except for the link to XR1200.
Here's the best advice I can give ya without knowing too much about your project structure.
I'm hoping you are using VS2010 so that things will look the same for you as on my machine.
One easy way to get the path of your master page all figured out is to add a test web page from masterfile, the path will be preset for you and you can use that in your existing pages.
Here is my blogs project structure.
Right click the folder where you want to add the test page and click add new file.
Add a new web form using Master Page. This is just for testing.
Now locate your Masterpage and select it.
From here just find your newly added content page in your solution structure and check the code files for the path to your masterpage.
Once you've verified that your path is correct, if it still bombs once you publish it then you've got either an issue where not all of your projects are building properly, or you're files aren't being uploaded properly and that is going to end up being something that you may just have to find on your own. I still suggest blowing away anything in your pub folder on your server and trying a clean publish.
Hope this helps!
my guess is that the iis setup is different on the different machines. typically, you see something like this when you have one machine set up with the site itself as a website and another machine set up with the site itself as an application within a website (like the Default Website). it looks like you have addressed this possibility by using the ~ in the path, but perhaps the iis on which you are deploying does not have the site directory itself set up as either a website or an application, but is merely a subdirectory of a site. in that case, the ~ would refer to the website directory (perhaps named xr?) and the site is in a subdirectory of that named xr. if this is the case, the solution is probably to set up iis such that you have the application directory configured as an application on the server to which you are deploying.
I'm currently working on an ASP.NET 4.0 site using a project-less solution.
By default the global.asax does not have a code-behind file, but after I changed it to
<%# Application Language="C#" CodeBehind="Global.asax.cs" Inherits="MyNamespace.Global" %>
And created an empty Global.asax.cs with the same namespace / class name I'm getting this error at compile time
Error 1 Could not load type 'MyNamespace.Global'. C:\Projects\RiskOptix\Code\RiskOptix.WebApplication\RiskOptix.WebApp\Global.asax 1
I've already tried cleaning out my entire bin folder but to no avail - this is extremely infuriating.
This question has already been asked. Check out this answer. Web site projects work differently from web application projects. Website type projects do not have CodeBehind files instead have CodeFile.
<%# Application CodeFile="Global.asax.cs" Inherits="MyNamespace.Global" Language="C#" %>
CodeBehind = Needs to be compiled ( asp.net 1.1 model) and compiled binary is placed in the bin folder of the website. You need to do a compile in visual studio before you deploy. Good model when you do not want the source code to be viewable as plain text ... for example when delivering to a customer who you not have obligation to provide code.
CodeFile = You provide the source file with the solution for deployment. ASP.NET 2.0 runtime compiles the code when needed. The compiled files are at Microsfot.NET[.NET version]\Temporary ASP.NET Files.
I was not able to run my MVC Web application. It was giving Could not load type <application namespace.Classname> error in Global.asax
I went to Project Properties and set the build>output folder to bin/ which was bin/Debug. Ran it once. It ran fine. And then again set the output folder to bin/Debug. Working fine now.
just to add my 2 cents with a WTF moment, My version of this error was caused by the Global.asax.cs not being included in the Visual Studio Project.
Right clicked on the .cs file, include in project and voila...
HTH
Dave
Yet another way to get this into this problem...
I had my web app open in VS2010, and IIS Express could run it just fine. Later, I opened the same web app, but in a newer branch with VS2012, and the virtual dirs in IIS Express auto-magically updated themselves to the physical dirs for the VS2012 project, without warning. So when I hit F5 to run my web app in VS2010 where I'm debugging, then I got the "could not load type or namespace" error in the global.asax file on one Import Namespace line. Closing both VS instances and reopening just the VS2010 version fixed the problem.
I'm running in to a lot of inconsistencies with regards to what will compile and execute correctly in a Visual Web Developer 2008 Express environment and what fails on my web server.
In particular, I've got an aspx and aspx.cs codebehind, plus a number of additional .cs files in my Web Developer project. It builds fine and executes okay under the Development Server.
Once I upload the files to my server, the codebehind doesn't seem to be aware of the other .cs files.
What's the correct way to make my aspx app inherit additional .cs files?
--- Update ---
Since I'm not really finding the answer I need, let me be a little more explicit with what I'm doing:
I have three files:
Default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Import Namespace="UtilClasses" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Default.aspx.cs
public partial class _Default : Page { }
App_Code/UtilClasses.cs
namespace UtilClasses {
public class AClass {
public const int A = 1;
}
}
In this example, if I attempt to do any of the following, I'll get a compilation error on my web server:
Reference the App_Code with a #Import
Call the code contained in it from the aspx.cs codebehind or
Call the code from the aspx page.
It compiles fine in Web Developer 2008. The soltuion is a Web Site which is auto published to my web server via FTP. The exact list of files being published are:
Default.aspx
Default.aspx.cs
App_Code/UtilClasses.cs
web.config
It seems like you have a deployment problem.
Just publish (in build menu there is a publish item) your web application/site to a folder and then move the files in that folder to your server.
I think you have old assembly files and new classes in your directory.
hope this helps
You can use partial classes. Add a file ( *.cs) and define your class as partial. So that, you can distribute your methods,properties anaother files
This is a very common problem of organizing code in your web applications. Here are some pointers to help you attend to your problem.
If using a single project and it being ASP.net website project, put your .cs files in the app_code folder.
Use namespaces wisely and keep track of what is in which namespace. If u have something like Resharper it saves this task.
Ideally you should have only page related and util classes in your web project. Create libraries using C# express for other functions. Test these libraries using NUnit/MbUnit or the likes.
If you have doubts if something runs on Cassini but not in IIS. A dev or local IIS is the only solution.
Hope that helps!