Tip of the day: Double question mark – Mikael Söderström

This tip on using the double-quotation marks in C# helped me a bit today when it came to integrating new code for an ASP.Net web application with legacy data access code that I was unsure about the quality of. The data access code that I was referencing  may or may not have null value checking, and there was no time because of an impending deadline to do code-review or quality control revision on it, but by using the ‘??’ check on values returned by it and some constant parameter values hydrated by web.config appSetting values II was able to implement default behaviour in my code for objects which did not get what was expected from the data access layer.

Of course this is just a band aid. The solution really lies in cleaning up that misbehaving code and defining standard contracts for behaviour between tiers that all the software team agrees to and which consistent code reviewing will reveal deviations from at earlier stages before integration, and perhaps the whole team becomes better developers because of it since we’re discipling ourselves on good practices for writing quality code.

SQL Server 2005 – Troubleshooting Orphaned Users

I always seem to keep forgetting when I backup from one server and restore to a new server that SQL Server 2005 will recreate the users from the original server, and they become ‘orphaned’ as they don’t match to the similarly named user on my system automatically, and thus cause an error when an ASP.Net application tries to use its web.config settings to access the database.

This Microsoft Support article gives the steps needed to fix this orphaned user issue as well as create it in the first place, so this is a cool resource to have when training others on the issues they may face and how to mitigate them when wearing their database administrator hat.

Quick and Dirty ASP.Net on Linux – Ubuntu, Mono and MonoDevelop

Having become inspired by the recent TTLUG activity in finally setting up the new TTLUG website, as the group’s Trustee I found myself ashamed that I did not even have a Linux machine readily available to hack anymore. My work took me so far into the C#, ASP.Net, and now ASP.Net MVC worlds, that I had little time for Linux or Linux-based Open Source software. Thankfully, fate stepped in.

I saw my coworker about to throw away several old IBM T23 Thinkpads. Now if you know and love classic hardware like I do, the words “throw away” and Thinkpad just don’t come in the same sentence, those babies are beasts. I managed to get two of them, and 2 spare batteries.

On bootup at home one of these machine’s hard disks failed while Ubuntu was installing to its hard disk, however the other took the Ubuntu 8.04 CD installation with quite a surprising speed for a lowly Pentium 3 with 256MB RAM. After doing the expected setup without hassles and updating the distribution via the Internet (using a  network cable since there is no built-in wireless in the T23) I rebooted and now had a full Ubuntu desktop system ready to go.

The first thing I was curious to see if I could do was build and run a traditional ASP.Net web page and get it running on Linux. I had tried several years ago and the process was painful and I ended up giving up before I succeeded in resolving all the issues. I was pleasantly surprised then by the *almost* painless experience this time around.

Launching Synaptic Package Manager, a quick search in it for Mono revealed the package I needed to install, and I saw the results also included the package for MonoDevelop, a RAD tool I used in my first experience all those years ago. I instantly clicked on it, not only because I had liked the tool, but because of a rule of thumb I learnt when dealing with smart installation tools like apt and its GUI-frontend Synaptic Package Manager i.e. when not knowing exactly what to install in Linux to get developing with a language, installing an IDE for that language should ensure all the dependency installation packages were also checked.

After clicking apply, and the downloads had installed I ran MonoDevelop, and used the menus to create a new ASP.Net Web Application in C#. A little browsing through the buttons at the top menu revealed the Run button (the one with the cogs), and this is where my first hurdle was hit. The build failed, reporting that “Build failed: Executable not found: /usr/bin/gmcs”. Undaunted, I did a quick Google search for the error, which resulted in a link to this Ubuntu forum thread, which indicated if I installed the package manually it would take care of the issue. Another answer in this thread recommended I also install automake.

After starting a bash Terminal and running the commands

sudo apt-get install mono-gmcs
sudo apt-get install automake

I went back into MonoDevelop and tried to run my web application again. This time the application compiled, however when it tried to run I was hit with an error window stating ‘xsp2 server not found’. Learning immediately from the previous error that it was probably a missing dependency I went back to my Terminal and typed the command

sudo apt-get install xsp2

which installed the xsp2 server and its dependencies. Again in MonoDevelop I clicked Run, and was quite happy to now see my test default page open up in Firefox, with xsp2 hosting it at localhost:8080.

Now that I’m up and running, I’ll probably be browsing the Mono project site a little more often for tips and hacks I can use with my new Ubuntu system using my experience as an ASP.Net developer. Hopefully by the next Ubuntu release someone who knows how to will have fixed the bug that left out these few important files from the present installation and iron out the current kinks in the system.

Customising the debug and run an ASP.NET MVC Application

I tend to use F5 to debug my code, my two teammates have a preference to have the codebase as a virtual directory in IIS and make changes to their code in Visual Studio and then reload the page in a browser to analyse the returned results.

Which is the better technique? Certainly my co-workers had a  point that by running using Cassini instead of IIS I was opening up myself to bugs popping up when the application was deployed to a real IIS box on testing and some IIS-specific behavior arose.

But why must I hassle myself with attaching IIS to the W3wp.exe process every time I want to do a line-by-line debug my code? And I always want to be debugging, especially in this new realm of integrated ASP.Net MVC where debugging a View means you have to worry about if the bug lies in your ASP.Net code, your JQuery code, or the service(s) your .ajax or .getJSON calls are making. I didn’t mind the hassle of having to recompile every time a change needed to be made inside the Controller or Business Logic layers once it meant I could track the state of my application during events as closely as possible.

Turns out we can have it both ways. These recommendation from Stephen Walter, although set on the topic of running an ASP.Net MVC Application specifically, are useful for any developer looking to customise the debug and run process when using Visual Studio 2008. So my coworkers and I will both be happy, because now they’ll be readily able to debug their code in real time (and engaging in a better coding practice at the same time) and I’ll be running my debug sessions on top of IIS to remove any possible hiccups from using Cassini for testing.

The settings are available on the Web tab on the Web Application Project’s properties page (ASP.Net MVC or not)  and to do this I have to make sure it is set to use a local IIS virtual directory (which I’ll make right there if it isn’t set already) and that I enable the check box that allows me to make code changes without the need to stop debugging and recompile.

Getting the address of the hosting server

One of the simple problems I’ve encountered was just getting the name of the server that’s running my ASPX application, whether it be the localhost:xxxx of VS 2008’s Cassini server, localhost, or http://www.myserver.com. The following commend returns that as a string.


The ServerVariables NameValueCollection has a very interesting collection of information that I can see being quite useful to know about in future.

DotNetJunkies has a good tutorial on ServerVariables in ASP.Net.

Note: The command above only returns the address as a string, not the protocol. If you want to use it in an anchor tag (<a href=”someaddress”>somelink</a>) don’t forget to append the protocol (e.g. ‘http://&#8217;) to the string returned from the command or you may experience some strange behaviors.

HOWTO: Convert BlogEngine.NET from Web Site to Web Application Project

Much thanks to BenAmada for providing on this thread a step-by-step guide for converting the downloadable BlogEngine.Net Web Site Project to a Web Application Project.

This varies a bit from the MSDN Documentation walkthrough on moving an ASP.Net Web Site Project to a Web Application Project as there are some added steps due to several reasons in the architecture of BlogEngine.Net e.g. the way BlogEngine does themes, its widgets framework and controls dependencies. These are not to criticise the design decisions made, it still is a full featured, well built blogging application which I use and customise daily.

Here are the steps repeated, but I encourage anyone benefitting from this to post your thanks to Ben on this thread as well so that he knows.

1. Create a new WAP.  File -> New -> Project -> Visual C# -> ASP.NET Web Application.  Select the location (I created a new folder for this WAP project), and I used ‘BeWap’ for the Name of the project.

2. Close Solution.

3. In Windows Explorer, copy all the BE files into the WAP folder.  You’ll be overwriting a couple of the files that were included in the new WAP project we created (default.aspx and web.config and maybe a couple of others).

4. Rename the App_Code folder to Old_App_Code.

5. Reopen WAP solution in Visual Studio.

6. Right-click on project name (BeWap) in Solution Explorer, and select ‘Add Reference’.  Go to the Browse tab, and browse to the BIN directory in this project, select ‘BlogEngine.Core.dll’.

7. In Solution Explorer, click the ‘Show All Files’ icon.  All the files/folders that we copied into the WAP folder will show up in Solution Explorer, but they are not yet included in the project, so they are white/ghosted out icons.  Select all these ‘excluded’ folders/files in Solution Explorer, right-click and select ‘Include in Project’.

8. Select the top node in Solution Explorer, ‘BeWap’ (right under Solution ‘BeWap’).  Right-click and select ‘Convert to Web Application’.

9. Save all files (Ctrl-Shift-S).

10. In the web.config file, in the <pages> section, there’s this line:

<add namespace=”Controls” tagPrefix=”blog”/>

Change that to:

<add assembly=”BeWap” namespace=”Controls” tagPrefix=”blog”/>

… where “BeWap” is the name of this WAP project.

11. The site.master files for Indigo, Mobile and Standard all have the same class name of ‘site’.  Change the class name in site.master.cs for the Standard theme to site_standard and changed the Inherits attribute in the <%@ Master %> directive in site.master to site_standard (so they match).  Need to do the same thing for the Indigo and Mobile themes … giving them class names of site_indigo and site_mobile.

12. The class name in edit.ascx, edit.ascx.cs and edit.ascx.designer.cs files in the RecentComments widget is widgets_RecentPosts_edit.  Need to change the class name in these 3 files to widgets_RecentComments_edit.

13. The class name in edit.ascx, edit.ascx.cs and edit.ascx.designer.cs files in the TextBox widget is widgets_LinkList_edit.  Need to change the class name in these 3 files to widgets_TextBox_edit.

14. The class name in widget.ascx, widget.ascx.cs and widget.ascx.designer.cs files in the TextBox widget is widgets_LinkList_widget.  Need to change the class name in these 3 files to widgets_TextBox_widget.

15. Save all files (Ctrl-Shift-S).

16. When trying to build the project, I was getting about 47 errors about unrecognized controls.  The problem was the ‘designer.cs’ files didn’t get created for these ASPX pages back in step 8 when I did the ‘Convert to Web Application’ command.  Even if I try repeating that step again, the designer.cs files still do not get created.  Visual Studio is complaining that various BE controls (blog:SearchBox, blog:PageList, etc) are unknown server tags.  If Visual Studio can build the project once, then these BE controls will start to get recognized.  What I did to get this done was temporarily exclude these pages that reference the BE controls.  The pages referencing these controls can be found in Visual Studio’s error list when you try to build the project.  I excluded the entire Widgets folder, the entire themes folder, the post.aspx file, the error404.aspx file, the default.aspx file and the apiTagMiniView.aspx file.

17. Build the solution.  Should get no errors.

18. Add those temporarily excluded items back into the project from two steps ago.

19. Select the top node in Solution Explorer, ‘BeWap’ (right under Solution ‘BeWap’).  Right-click and select ‘Convert to Web Application’.  The pages we re-included back into the project should now all have designer.cs files.

20. Build the solution.  Should get no errors.

21. Run the solution.  Everything should work.

ASP.NET: 10 Tips for Writing High-Performance Web Applications

It’s always good to keep refreshing yourself on the first principles of web application development, especially if you’re like me and given a large code-base not of your writing but for your modification and optimisation.

I recently started looking at some code hacks I hadn’t quite been exposed to before…in order to ensure I wasn’t going to rewrite for rewriting’s sake and was not limiting myself to just the coding practices I knew, I consulted the ASP.NET: 10 Tips for Writing High-Performance Web Applications as my refresher before analysing these bits to determine if they were indeed priority candidates for change in order to make the web application more stable.

Now if only I could find a Web Application Development Bible specifically for ASP.Net MVC development…

Creating Your Own Web Site Administration Tool in ASP.NET

One of the challenges faced trying to scale and customise BlogEngine.Net was that the user administration pages were never designed for a large volume of registered users, as evidenced when one uses the administration pages. You’re basically stuck with just the first page listing of 50 or so with no way to access users beyond this number. This is definitely no good for a website carrying tens of thousands of registered users.

The initial idea I had was to try to port the ASP.Net Website configuration tool that Visual Studio has into my project. During research for this though, I found a link that helps you create your own Web Site Administration Tool in ASP.NET and this was quite easy to follow.

Continuing this research, since although easy to do I was not fond of reinventing the wheel especially if there existed a better wheel already, I found an article on ScottGu’s blog on an ASP.Net 2.0 Membership and Role Management Remote Admin Tool which sadly contained links to no longer existing 3rd party articles and seemed to dead-end this research.

Through more active Google searching though, I found what I was looking for, the ASP.Net WSAT (WebSite Administration Toolkit) – Membership Administration project. As icing on the cake, this project has recently released at updated .Net Framework 3.5 version which did far more than what I wanted, and looked quite mature and hence suitable to use.

The issue now was to integrate it properly with BlogEngine.Net’s structure, and this is where Sueetie came in. This movement is aimed at promoting online communities using .Net Open Source software. Their associated Codeplex project contained custom change code and instructions for integrating the WSAT project with BlogEngine.Net (along with several other nice integration solutions, such as a Media Gallery, Wiki and Discussion Forum also powered by .Net Open Source software.

So now it’s just to follow the guides, and hope for the best. There’s still a bit of work to be done, but thankfully nowhere near as much as building this from scratch!

Update 04-02-2009: For some reason the owner of the WSAT codeplex project closed it and removed all the download links. Just when I was about to start working on it!! I’ll attempt to contact the owner and see what this is all about, and whether or not he can re-release the downloads.

Update 05-02-2009: Having had no response from the project owner, and facing an impending deadline, I chose instead to use another, less pretty, ASP.Net Membership Administration solution published written by Dan Clem of 4 Guys from Rolla fame. It has less features than the removed ASP.Net WSAT project solution, but it will do the essentials of user management that were my initial goal.

DateTime.ToString Method

DateTime.ToString Method.

It’s always good to remember what a useful override of the ToString() method on any object can do. Perfect example is C# DateTime whose ToString() method can take parameters that determine the formatting of the returned value.

Examples for DateTime’s .ToString(“format“) method include the following (where format is replaced by the coding value assigned):

d :08/17/2000
D :Thursday, August 17, 2000
f :Thursday, August 17, 2000 16:32
F :Thursday, August 17, 2000 16:32:32
g :08/17/2000 16:32
G :08/17/2000 16:32:32
m :August 17
r :Thu, 17 Aug 2000 23:32:32 GMT
s :2000-08-17T16:32:32
t :16:32
T :16:32:32
u :2000-08-17 23:32:32Z
U :Thursday, August 17, 2000 23:32:32
y :August, 2000
dddd, MMMM dd yyyy :Thursday, August 17 2000
ddd, MMM d "'"yy :Thu, Aug 17 '00
dddd, MMMM dd :Thursday, August 17
M/yy :8/00
dd-MM-yy :17-08-00

CSS: fixed menus vs AJAX AlwaysVisibleControl

So after several days of fruitless research into understanding why my implemented control which utilised the Microsoft AJAX Toolkit’s AlwaysVisibleControl would render on my developer’s box but not my test deployment server, I finally gave up and went back to my roots, opting for use of a pure CSS solution, which led me to a great resource page for CSS: fixed menus

Hopefully there are no ‘bite-me-in-de-arse-at-release-time’ issues with using the ‘standards’ CSS coding approach vs the Microsoft approach of using AJAX Toolkit controls for an IIS hosted solution. This might sound paranoid, but this is exactly what happened to me some time ago when I used pure HTML controls in a web page rather than their ASP.Net control equivalents resulting in some rather irratic rendering behaviors and a good few hours more than I had planned of debugging.

However this experience does reinforce my opinion that a true master of ‘Microsoft web programming’ hasn’t just mastered knowledge of ASP.Net tools alone, but also knows his HTML, CSS and Javascript, to me the traditional ‘standard’ web development arts, well enough to hold his own. That knowledge definitely led me today to an alternative solution to a hindering problem.