I know if someone wants to re-use some classes (not UI), he must gather all of them and put in a Visual Studio Class Library, build it to some dells and distribute these dlls. In this approach there just one code, you just update code in one place.
But what about ASP.NET's markups? For example you have an .ascx file or a collection of .aspx files regarding user management. If I want to use them in another project I am forced to copy them in new project again. By this I have two same code that is very hard to maintain.
So is it any way to re-use .ascx and .aspx files just like simple .dlls? For example building them?
Many Thanks,
Afshar Mohebbi
With the default configuration, .ascx and .aspx files will need to exist on disk, because they need to have a path associated with them for everything to work. All the code (everything but the first line which specifies which class to inherit) in them, however, can be compiled away into a DLL file. It would probably be possible to get around this by writing custom handlers and build providers that load things from DLLs, but it's not worth the effort.
If you want to put your user controls into a DLL file, create them as custom controls instead of user controls (.ascx files). That's how all the custom control libraries for sale around the 'net are done.
Related
So I imported this aspx page done by a former dev who worked for the company I'm in now. I found that the aspx page left by him doesn't have a codebehind file so I assumed this wasn't the source code. I can't find the source so I added a code file and try to work it out on my own. But my main concern is this: clients can't access the code behind, right? Is a manually added code file subject to the same protection?
The codebehind file is there as a place to put your server side code. However it's technically not necessary to have one since you can put the code in the aspx file using c# script tags. It's however recommended to put it in the codebehind file for better separation between markup and code.
It does not matter if you add it yourself or if Visual Studio adds it for you. It does not change anything in terms of access. In all events it executes on the server.
If your server is properly configured to run ASP.NET applications - which I believe it is - then IIS will not serve .cs files to a client. These will normally be accessible only through FTP. Try it yourself, by browsing to any .cs file in your application :)
Also notice that what you get when you browse to an .aspx file is not the very same code you'd see in Visual Studio, but the result of that being processed. IIS will serve the resulting HTML. So even if you have server side code in the ASPX file, that won't be visible to an end user browsing through your application.
Sounds like a web application project; in this case, the code is in the code-behind file as #TGH mentioned, and the code would be in the DLL compiled for the web application. Therefore, the only way to get that code is use a tool like Telerik JustDecompile, and decompile that DLL to grab the source code for EVERY file in the project. It would be much better to have the source, as these decompile tools do not include everything in that code-behind file.
The aspx page that I need to add code to has an aspx.cs file which was written by a company that has this aspx.cs file on their system (per contract) and I cannot modify/access.
Example: MyFile.aspx and no access file MyFile.aspx.cs
I need to use a label.text from the page in the control pages and also depending on IF ELSEIF statements it will call which of the different controls that it needs to execute.
What is a good way to do this when one does not have access to the aspx.cs file?
I have spent a couple days trying to find/figure out answers to this problem and keep running into problems.
You do have a strange situation here ;)
Here's one wild idea. You can change the Inherits attribute at the top of the .aspx file to substitue your own class instead.
Of course that means you have to rewrite all the logic behind the page -- or you can try to have your own class inherit the original one, but depending on visibility of original code this may not work.
Another option could be to use a decompiler to re-create all the source code of the web project from the compiled dll. But you may not be legally allowed to do that -- although I would point out that if you don't have the rights to use the code, you probably don't have the rights to use the .aspx either, even if you have access to them.
I recently inherited a solution with about 10 projects in it. 6 of these projects are individual websites that are basically copy/pastes of the original.
This means that any changes that need to be made must be made to each project in order to update all the websites.
What I want to do is have one project for the website code and be able to deploy that code and some configuration settings to create a new website. That way when I make updates to the main Web project I can just deploy to all the websites.
How do people normally approach this? I'll outline my thoughts on it and hopefully some of you can point out better ways to accomplish this or at least give me some affirmation that I am on the right track.
Have a master markup with very general containers.
Allow the users/people setting up the site add widgets to the site which will be assigned to widget placeholders at the top and bottom of all the generic containers.
All styling and colors will be controlled with a stylesheet that can be swapped out.
I know there is some kind of theming you can do. Does this just swap out groups of css and let you configure which one to use in the webconfig?
For elements that will be the same across all sites such as footer images have a naming convention. So if I want Site A to have some footer image I just replace the footer.jpg in the project when I deploy.
Your approach is good.
You should use master markup
Your web system will be CMS
You can use themes to define styles of different projects or you can link css files dynamically
if they are really copies of each other than you can make it very general by defining all of them as one website and put all the settings of css images markups in database
If you don't want to make everything very generic you can make web user controls and load them dynamically at run time according to the project (Remember, you can load WebUserControls at runtime using the LoadControl method)
That's unfortunately a question that is likely to get the response 'It depends' as each of those approaches could be used or not used dependent on the needs of the project. If the projects only vary by presentation then master pages combined with CSS would make a reasonable solution.
I have a fix done for my user control.
The changes are there both in the .ascx file and the .ascx.cs file.
Now, post-build...is there any way I can deploy just my...assembly for this control container...and avoid the deployment of the .ascx file?
Although the assembly contains some pre-compiled methods, the .ascx page is still "interpreted" (technically it's compiled, but IMO it's easier to think of it as interpreted) every time the page is requested (well, not every time, since it's often cached, but certainly the first time changes are made). Try it for yourself -- make a change to some of the markup in a .ascx file and then request the page -- you'll see that your changes are reflected in the response (you may have to restart the site if it's cached).
So, following from this, you will need to deploy both of the files, as both are used by IIS to service a request.
On another note, you can sometimes run into strange problems when just deploying a single assembly (or maybe it's just me being paranoid because I thought this happened to me once) to a live site -- it's safer to deploy the entire site at once. Unrelated to the question itself, but I thought I'd note it.
no .. if you have done change in the .ascx file you should upload that too..
Because the ascx file has changed and as it's markup code does not go into the compiled assembly of the control, for reflecting the changes, you'd need to deploy both the assembly (which contains the ascx.cs code) and the ascx file.
We have a bunch of user controls we would like to pull out of a web application and into a separate assembly/library, and I thought it would be as simple as creating a class library and pulling the ascx and ascx.cs files into the project, and compiling a DLL to be reused among our applications.
This was not the case, however.
Our ultimate goal is to have a single distributable DLL (similar to how Telerik distributes their controls) that we can throw into any web application. The steps here: Turning an .ascx User Control into a Redistributable Custom Control were very simple to follow, however this results in many files named controlname.ascx.guid.dll, which is not the desired result. I couldn't even get these to work anyways, since we have additional classes that need to be compiled into the assembly.
Has anyone successfully created a web user control library in .NET (we're using 3.5 here)? I can't seem to find a nice step-by-step guide.
I realize this is an old topic, but if anyone is looking for a solution for creating reusable user control libraries, it turns out it's fairly simple. Here are two good step-by-step guides along with source code:
From MSDN: Turning an .ascx User Control into a Redistributable Custom Control
From Code Project: Straight way to create ASP.NET user controls library
The second link provides a solution to the multiple dlls created by the first link.
Edit- (2) Seems to be a dead link. Here's the new link
https://www.codeproject.com/Articles/30247/Straight-way-to-create-ASP-NET-user-controls-libra
If you want to share controls among project, my experience has shown that the best way is to build custom asp.net server controls instead of usercontrols. User controls are good for sharing within the same project, but not over multiple ones.
For this purpose I suggest you to build a set of custom server controls inside a class library and use that on all of your projects.
This book does quite a good job at explaining the basics of creating server controls
Edit:
I'm currently developing a .net web server control library. I actually didn't follow any step-by-step guide. I mostly considered using the book I mentioned above and the MSDN library + Reflector, which is a great tool for inspecting existing MS server controls and learning from them.
I found the tutorial Creating and Using User Control Libraries but it seems like a bit of a hack as it relies on a post-build command line event to copy the user controls from one project to another.
Somewhat late, I admit.
To create a re-usable library of user controls; create a new Web Application Project, delete all the scaffolding, add a (number of) user control(s). Create a Web Deployment Project from the Web Application Project, in the WDP properties choose the option to Merge all control output and assign a name for the library and ensure that Allow this website to be updatable is NOT checked.
Build the WDP and use Reflector to examine the generated library; you'll see that it contains an ASP namespace and the types you carefully crafted have been renamed i.e. usercontrol_ascx. In your target website(s) add references to BOTH the output dlls from your WDP, add a system.web/pages/controls node to web.config using the namespace ASP and the name of the assembly that you defined in the WDP.
Now when you use the library in a page (for example) you must use the alias that you defined in web.config and the typename as seen in Reflector i.e.
<ucl:usercontrol_ascx ... />
I found it useful to add a dependancy for the website(s) on the WDP so that the WDP is built before the websites; now I can change the user controls in the WAP without having to remember to build the WAP before building the website(s).
I hope that someone finds this useful as it cost me a few grey hairs getting to this stage and still have VS do its 'automagically' thing.