Difference between "codebehind" and "source" - c#

Can any one tell me with example what's the difference between Codebehind="MyCode.aspx.cs" and Src="MyCode.aspx.cs"?

CodeBehind
Specifies the name of the compiled file that contains the class
associated with the page. This attribute is not used at run time. This
attribute is used for Web application projects. The CodeFile attribute
is used for Web site projects.
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.
Src
Specifies a path to a source file containing code that is linked to
the page. In the linked source file, you can choose to include
programming logic for your page either in a class or in code
declaration blocks.
You can use the Src attribute to link build providers to the page. For
more information, see the BuildProvider class. Also, in versions of
ASP.NET prior to 2.0, the Src attribute was used as an alternative way
to link a code-behind file to a page. In ASP.NET 2.0, the preferred
approach to linking a code-behind source file to a page is to use the
Inherits attribute to specify a class, along with the CodeFile
attribute to specify the path to the source file for the class.
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 Microsoft.NET[.NET version]\Temporary ASP.NET Files.
Its always recommended to check the official documentations first. See the msdn documentation for this Question.

codebehind is a particular particular technique born with NET.
This allows you to write application code by separating it from the graphical presentation.
For example:
<script runat="server" src="MyCods.cs" />
execute a primitive code-behind. this because the code isnot compiled and not generate. For This reason remain a simple format plain text.
Il codebehind is default applied in the major IDE as:
Microsoft Visual Studio 2003 - .net 1
Microsoft Web Matrix - .net 1
Microsoft Visual Studio 2005 .net 2
Microsoft Web Developer Express .net 2
This implies that the association of the source code, in the form of DLLs, the page to which it is related. I remind you that each page is viewed. NET as a single class, which will inherit the code derived from. Previously compiled dll.

Related

Imported website into web application in visual studio gives 'Type typename already defines a member'

After importing this existing (and functional) site into Visual studio (tried 2017 and 2019 both) running .Net 4.0 I am getting a plethora of not found errors.
Codebehind pages are not able to see elements on the existing page
Classes are declared to be duplicated or ambiguous
Function names (both standard like Page_Load and custom) are reported as already defined.
I am sure that all these items are related. Here are the steps I followed:
Created new Web Application from Visual Studio 2017 template
Copied solution and csproject files into existing website folder
Added files into the project
Re-added references to site
Set custom class .CS files to Compile
Removed "unnecessary" using statements (which were already declared in web.config)
Create a new ProfileCommon stub referencing ProfileBase
Beyond all this I have tried renaming a number of these functions and classes (using the rename) to create a new reference point.
Not sure what else I can do on this beyond major changes or a complete rewrite (100+ pages of code so far) by creating the new class files then copy/paste.
What other options are open to me?
Ok, this is is a migration project. Only you can "determine" if you want to convert from a web site to a web site application, and the time and efforts for such a migration project.
Now, having stated the above? No question, that I prefer hands down a web site applications. Simple things like have VS compile and build the application, ability to add assemblies (and not have the mess of .dll's HAVING to be in the bin folder to resolve references - yuk!).
And then simple things like the ability to multiple projects in one project are things I come to enjoy, and expect over the years when developing software.
About the "only" thing going for a web site? Your deployment is oh so easy, since you can open a web page, or code behind, hit save, and you are done!
but, this means both source code (code behind) has to be deployed to the web site. As noted, I prefer a build and compile process in VS. This does mean that to make a "minor" change, then you have to do a full re-deploy of the whole site.
However, these benefits I like and enjoy? it not all a given that converting an existing web site to a web site application is worth the efforts. Only you can make this decision. On the other hand, you would not be doing this work unless you KNOW what you doing, and thus can determine if these efforts are worth the time.
Such a conversion and refactoring process is somewhat beyond a simple post on SO.
However some steps and tips are outlined here:
https://devblogs.microsoft.com/dotnet/converting-a-web-site-project-to-a-web-application-project/
I would consider starting over, and try the steps in above.
There is a "convert" option outlined in above, and this can save enormous amounts of time. It has been some time since I have attempted such a conversion, and I not tested the "convert" option in vs2022, and hopefully it still exists.
However, another possible option? Keep the site as web site.

How to decompile ASP.NET web application [duplicate]

we have an application in production and the code is in Pre-Compiled form. The developer who developed that application left the company and we don't have any backup of source code. the only access we have is Pre Compiled code in the server. We need to fix the issues in the application now.
Is there any way to Decompile (extract to actual source code) the PreCompiled code ?
Open the DLLs in the .NET Reflector.
To update #xOn's answer:
Due to a recent source control disaster I had to recover my project from the DLLs. So sadly I'm knowledgable on the procedure:
Get .Net Reflector from here: http://www.red-gate.com/products/dotnet-development/reflector/
You do not seem to need FileDisassembler.
Either load your bins in Reflector or double click them.
Mind that the .Net framework might have not maintained your original project file structure. So if some of your ASPX pages shared the same basic class name in different .cs files (I.E. Inherits tag is the same, but CodeFile tag differs), you would not be able to simply "export source code".
Don't go blindly exporting source files. It won't work.
There will still be some work to be done before being able to just fire up the old build button. For example - DLLs can refer to property's getter/setters directly. You will have to fix that.
Good luck.
Start with:
http://www.red-gate.com/products/dotnet-development/reflector/
...plus:
http://www.denisbauer.com/NETTools/FileDisassembler.aspx
You'll have to recompile the latter to link it against v6.5 of reflector (the latest, as of this post.) It is a good add-on if you want to avoid having to copy and paste ever single class into files by hand. It will dump an entirely assembly as .cs files. I think it may even generate a csproj.
Both tools are free.
I had the similar issue and used Reflector to Decompile it. I got the source code, then changed the bit I wanted, and rebuild it. Then I copied that dll again to Production site. It started to reflect my changes. It was very easy and not at all difficult, maybe because Precompiled site had dlls for every page, and was updatable , so had only code-behind file in dll.
For reference: http://www.reflector.net/
When u install .net reflecter.its import in visual studio.
then you saw the .net reflector tools on menu bar.
Click on .net reflector >>Choose Asseblies (Dll file) to debug.
I found that dll can be just Read using the .NET Reflector tool but can't extract the Source Code. And also once the webforms are precompiled, we can't even get back the code behind files. All we can do is to debug and analyze the Code.

ASP.NET Website to Web Application conversion issue - Could Not Load Type 'ClassName' AND Set Namespaces for all existing webpages technique

I have converted one huge Website Project into a Web Application Project as we want to have Compiled code in form of DLL for security purposes and followed below MSDN articles:
http://blogs.msdn.com/b/webdev/archive/2009/10/29/converting-a-web-site-project-to-a-web-application-project.aspx
http://msdn.microsoft.com/en-us/library/aa983476%28v=vs.100%29.aspx
The website is having 1000s of web pages/files. I have resolved all the issues of duplicate references, class name colisions and some compilation errors faced at on Build Solution stage. Due to the earlier website pattern, class names are in the following pattern:
for e.g. if UserMaster is in admin/Operations/Masters folder so the class name will be like: admin_Operations_Masters_UserMaster.
Now, Web Application is Building Successfully and when I try to run the application, it shows "Could Not Load Type 'ClassName'" so I found out that the issue of because Namespaces are absent due to earlier Website Pattern.
Found out in the MSDN:
By default, pages and classes that are built by using the Visual Studio Web site project model do not automatically include a code namespace. However, pages, controls, and classes that are built by using the Visual Studio Web application project model automatically include a code namespace. When converting the Web site project to a Web application project, you will have to add the namespaces to the code.
I have DEFAULT NAMESPACE in the project properties.
So I tried to add the namespaces to whole of the project via ReSharper -> Refactor -> Adjust Namespaces but its for .cs files only and not for .aspx.cs etc AND so its not working.
I thought I need to make a utility to add the namespaces automatically in pages as per their folder path structure but its a time consuming process.
So need a workaround to add the namespaces automatically in pages as per their folder path structure.
Secondly,
What I did for testing is, I have wrapped the default namespace in login page of the project where I faced 'could not load type issue', added in Inherits attribute also. Then did a Clean Solution then Rebuild Solution. But still getting the same issue.
I don't why DLL is not getting updated with the login page's class.
The strange thing is when I change the Codebehind to CodeFile (attribute of Page Directive) then the page is working very fine. Its due to source file is present at the time of running the project BUT i want to use Codebehind attribute as it will tell the compiler to look for the class in DLL not in Source file.
Finally, Summary is :
I have two major issues:
Could not load type 'className'
How to add the namespaces automatically in pages as per their folder path structure.
I have already searched here and came around different topics but nothing works for me. one have suggested to use "Surround With" feature of VS but I need to go one by one on each file which is NOT possible right now.
So Tried:
Set path of DLL to "bin" instead of "bin\debug"
Checked the Build Configuration and its of x86. also the DLL is present in bin
Checked all project properties and all are fine. Cleaned and rebuild solution hundreds of times. Cleaned Temporary ASP.NET files also.
Set Build Action to "Compile" for .cs class files
Cannot set Build action to Compile for aspx pages as they are static html and they should be set to "Content" which is default Build Action.
Resharper solution for adding namespaces but not working
Please help....
Any help will be greatly appreciated. Thanks.
Solved the Issue:
Earlier, I have tried to Set Build Action to "Compile" for .cs class files Only and for all the files sitewide to Compile from .csproj file but it was not working due to aspx page itself set to Compile which was not required.
Now, I have changed the Buid Action to Compile which was Content for .aspx.cs and .aspx.designer.cs files.
and it solved the issue.
Like for e.g.:
Solution 'TestWebApp1'
Project 'TestWebApp1' (ASP.NET Web Application)
Properties
References
App_Data
Scripts
Default.aspx (Build Action: Content)
Default.aspx.cs (Build Action: Compile)
SiteLayout.Master (Build Action: Content)
SiteLayout.Master.cs (Build Action: Compile)
Web.config
Source: None of my "code behind" code is being called

How T4MVC add-in and ASP.NET MVC integrate & work together?

I have an open source ASP.NET MVC application there is an add-in called T4MVC and I can see all the html, aspx, images, Controllers, views files are integrated, I wonder how it works.
If I create a file(aspx,html...) has it to be integrated manually to that template(T4MVC) or by default it get mixed? could someone please advise of how to get this(T4MVC) ans MVC do work together since I am new in ASP.net MVC.
T4MVC is a template that Visual Studio will compile and execute when you save the file in the editor (there are tools to automate this; search for AutoTT or Chirpy to check them out).
The template scans your project to find controllers, views, images, etc. and generates code that allows you to reference those items programatically elsewhere in your code. Whenever you add, remove or rename a resource (any of the items the template looks for) the template must be executed again to regenerate the code output.
The advantage of T4MVC (over the use of strings to reference things) is that you get compile-time checking, as well as intellisense and refactoring support.

Visual Studio, Razor, BuildProviders and Intellisense

I'm trying to get Intellisense working for razor views in a non-ASP.NET project and would like to understand the relationship between VisualStudio's Razor editor and BuildProviders.
For background, I'm writing a framework on top of Manos (mono web server) that uses Razor for its view engine. I've got that part working perfectly, but Intellisense in VS doesn't work giving a range of errors from unknown types to unregistered build providers, depending on where the output DLL's of the project are placed.
My project is a .NET Class Library, with .cshtml files (build action none). The base razor view class is defined in a separate assembly (outside the project) which could be registered in the GAC, but currently isn't.
I've already read these articles:
http://www.west-wind.com/weblog/posts/2011/Jan/12/IntelliSense-for-Razor-Hosting-in-nonWeb-Applications
Need razor view engine auto-complete to work in a class library?
http://blogs.msdn.com/b/webdevtools/archive/2011/01/20/how-to-get-razor-intellisense-for-model-in-a-class-library-project.aspx
.NET - Razor outside MVC application - Problems with removing #inherits and providing #model
Sounds like I need to write my own BuildProvider, but can't find any documentation explaining the relationship between a build provider and razor intellisense.
Razor intellisense is flaky at best currently. However, if you're using VS SP1, its slightly better. The web.config workaround (as pointed in your third link) works for me in a class library as long as the extension is cshtml (haven't tried vbhtml so can't say for sure).
Also take a look here: http://razorpad.codeplex.com/
Similar to LinqPad, this will allow you to test your razor code ahead of time.
The Razor editor is pretty heavily tied in to the ASP.Net runtime, in fact it actually runs ASP.Net in the background in order to collect the necessary run-time information.
My only suggestion for getting true-fidelity IntelliSense is a bit of a super-hack. Rather than a Class Library, you could make your application a Web Application Project. A WAP is actually just a class library which VS can host a website from. if you clean out ALL the extra stuff (Global.asax, web.config, etc.) you may get exactly what you're looking for. It's a workaround, and a bit of a stretch at that but give it a shot, it may just work :)

Categories