Recent posts

HttpUtility Class with ASP.NET

Everybody knows how to use the ASP.NET Server object. This object exists in different versions since Microsoft created Classic ASP.

You can use the ASP.NET Server object to decode/encode HTML, so it will be readable in your favorite browser. Or you can decode/encode URL strings for a reliable HTTP transmission between the webserver and the client. Nothing new I guess.

The ASP.NET Server object has methods and properties exposed by the HttpServerUtility class. The HttpServerUtility class is using the HttpUtility class internally.

If we take a look at the HttpUtility class, we can see different methods. Some of these methods can be very useful in some cases.
So before you start coding yourself some really nice code, please take a look at what the .NET framework is providing you for free. In that way, you can spend more time doing fun stuff! ;-)

HttpUtility.HtmlAttributeEncode - Minimally converts a string to an HTML-encoded string. You can use this for example with dropdownlists. (don’t replace strange characters on a itembound e.g.)

HttpUtility.ParseQueryString - Parses a query string into a NameValueCollection UTF8 encoding.

Example HttpUtility.ParseQueryString (C#)

    1 <%@ Page Language="C#"%>
    2 
    3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    4     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    5 <script runat="server">
    6 
    7   protected void Page_Load(object sender, EventArgs e)
    8   {
    9     String currurl = HttpContext.Current.Request.RawUrl;
   10     String querystring = null ;
   11 
   12     // Check to make sure some query string variables
   13     // exist and if not add some and redirect.
   14     int iqs = currurl.IndexOf('?');
   15     if (iqs == -1)
   16     {
   17       String redirecturl = currurl + "?var1=1&var2=2+2%2f3&var1=3";
   18       Response.Redirect(redirecturl, true); 
   19     }
   20     // If query string variables exist, put them in
   21     // a string.
   22     else if (iqs >= 0)
   23     {
   24       querystring = (iqs < currurl.Length - 1) ? 
   25             currurl.Substring(iqs + 1) : String.Empty;
   26     }
   27 
   28     // Parse the query string variables into a NameValueCollection.
   29     NameValueCollection qscoll = HttpUtility.ParseQueryString(querystring);
   30 
   31     // Iterate through the collection.
   32     StringBuilder sb = new StringBuilder("<br />");
   33     foreach (String s in qscoll.AllKeys)
   34     {
   35       sb.Append(s + " - " + qscoll[s] + "<br />");
   36     }
   37 
   38     // Write the result to a label.
   39     ParseOutput.Text = sb.ToString();
   40 
   41   }
   42 </script>
   43 
   44 <html  >
   45 <head id="Head1" runat="server">
   46     <title>HttpUtility ParseQueryString Example</title>
   47 </head>
   48 <body>
   49     <form id="form1" runat="server">
   50       Query string variables are:
   51       <asp:Label  id="ParseOutput"
   52                   runat="server" />
   53     </form>
   54 </body>
   55 </html>

Published: 28-04-2009 by Tim van der Weijde | 0 Comments | 0 Links to this post
 

Enterprise Library 5.0 is under development NOW

It’s a shame this year’s Microsoft Developer Days does not cover anything of the work the Patterns & Practices team is doing. However I know from first hand that they’re doing a very good job. A few weeks ago, you were given the opportunity to vote for your favorite feature or improvements, and now, they’ve delivered their first drop. And while you’re reading about it, notice that P&P is actually using (a variation) of SCRUM, hence their reference to Sprints.


Published: 22-04-2009 by Dennis Doomen | 0 Comments | 0 Links to this post
 

Be nice, only give a thumbs-up

Although the wildcard proposal system allows you to give a thumbs-down, please don't. If you do, the votes can never give a realistic view on the popularity of the proposals. If you like more than one proposal, just vote for all of them, but don't punish anybody else.

Published: 20-04-2009 by Dennis Doomen | 0 Comments | 0 Links to this post
 

Some notes on wildcard proposals

If you want to vote for a wildcard proposal (and for obvious reasons, I encourage you to do that), you must complete the following steps:
  1. Register yourself (if not already done) and process the activation email.
  2. Logon
  3. Go to the wildcard proposal
  4. Click the Thumbs Up icon next to the speaker's picture

In other words, it's not sufficient to add a new comment. You must click the Thums Up icon.


Published: 15-04-2009 by Dennis Doomen | 0 Comments | 0 Links to this post
 

We've just passed more than 1000 unit tests

In November 2008, me and some fellow colleagues have started working on a new project where we use Test Driven Development to gain a highly maintainable and highly testable system. Today, we have reached our 1000st unit test,  or better, 'specification'. The term Test Driven is a bit misleading since TDD is really a design process, which has nice little granular unit tests as a side-effect. Anyway, we tried to reach this point before Easter, but obviously we were professional enough not to add some dummy tests :-)
 


Published: 14-04-2009 by Dennis Doomen | 0 Comments | 0 Links to this post
 

Wildcard Proposal: TDD and SOLID, two ingredients for high quality software

Many attempts have been made to improve the overal quality of our software development efforts, but if there's one I'd like to put some attention on, it's Test Driven Development. It's design-first, test-first approach has proved significant increases in overal quality. However, TDD is not easy and requires quite some understanding of proper object oriented design. S.O.L.I.D. is one of the better known acronyms referring to a set of very imporant design principles that both improve your ability to do TDD, but also make sure that your software is highly testable and maintainable.

If you like to hear more about this on the coming Developer Days, give me a Thumbs Up at the DevDays09 site and help me into one of those wilcard sessions.
 
Prerequisites: You should be able to understand C# 3.0 and Lambda expressions, and feel at home amongst the principles of OO such as encapsulation, inheritance and polymorphism.
 
Important Note: You have to register on the DevDays site, regardless of whether you're not sure yet you'll go. Then wait for the activitation mail, log in and press the thumbs-up symbol next to my photo and the vote count.
 

Published: 10-04-2009 by Dennis Doomen | 0 Comments | 0 Links to this post
 

IIS 6 & 7, Windows Authentication, and locally hosted websites using host headers

Like many developers at our company, I like to develop on a virtual machine. I ran into an interesting issue today with integrated authentication in IIS7. I have a Windows 2008 server VM  that I  use to develop Commerce Server 2007 projects on. On this machine, two websites are hosted: the first one being a Commerce Server 2007 website, the second being the webservices exposing Commerce Server functionality for the business applications and such.

Some of these webservices require that anonymous authentication is turned off. This is where I ran into problems: I kept getting a 401.1 and 401.2 authentication error. The machine resides in our domain, but I am using a local user account for the application pool that runs the website. The website was available using a host header (i.e. http://somename.local/. After a lot of research, and some help from colleagues (thanks Tim, Patrick), and trying almost every trick in the box l came across a Microsoft article that solved my issue. Basically, it came down to adding a registry entry:

1. Open a registry editor.
2. In Registry Editor, locate and then click the following registry key: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0
3. Right-click MSV1_0, point to New, and then click Multi- String Value. Type BackConnectionHostNames, and then press ENTER.
4. Type BackConnectionHostNames, and then press ENTER.
5. Right-click BackConnectionHostNames, and then click Modify.
6. In the Value data box, type the host name or the host names for the sites that are on the local computer, and then click OK.
7. Quit Registry Editor, and then restart the IISAdmin service.

 

After that, my problem was solved. For the whole knowledge base article, please click here.


Published: 07-04-2009 by Arnold Jan van der Burg | 0 Comments | 0 Links to this post
 

Help prioritizing Enterprise Library 5.0

Patterns & Practices has just finished compiling a list of candidate features and improvements for the next installment of the Enterprise Library. Grigori Melnik, the Enterprise Library's product manager has opened an online survey and invites the .NET community to share their priorities and, if needed, add any additional requests. Make sure you add your vote over here.


Published: 01-04-2009 by Dennis Doomen | 0 Comments | 0 Links to this post