Sunday, February 21, 2010

URL Routing in ASP.NET 4.0

In ASP.NET MVC you can have clean nice SEO friendly Url.
For example:
- http://mySite.com.au/Category/List
- http://mySite.com.au/Product/Detail

However with ASP.NET 3.5 SP1, Webforms require an implementation of an IRouteHandler to support clean nice Url.
This can be quite tedious, and often require more work.

However with ASP.NET 4.0, you can add this support easily without having to use or implement an IRouteHandler.

The below two articles explain how to achieve this in web forms.

http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx

http://www.4guysfromrolla.com/articles/012710-1.aspx

Happy Programming!

Saturday, October 3, 2009

Using C# 3.0 Anonymous Types as Dictionaries

public static string GetHtmlLink(string text, IDictionary properties)
{
//do some work
}

//too many lines and less efficient
Dictionary values = new Dictionary();
values.Add("key1", "value1");
values.Add("key2", "value2");
values.Add("key3", "value3");
GetHtmlLink("Click me", values);

//better
MyParams myParams = new MyParams { Key1 = "value1", Key2 = "value2", Key3 = "value3" };
GetHtmlLink("Click me", myParams);

Sample link: <%= HtmlHelpers.GetHtmlLink("My Site", new { @class = "someStyle", href = "http://www
And it'll render this HTML:
Sample link: <a class="someStyle" href="http://www.example.org">My Sitea>

<br />
Sample URL: http://www.example.org/search?query=kitten's+mittens&mode=details
from
http://weblogs.asp.net/leftslipper/archive/2007/09/24/using-c-3-0-anonymous-types-as-dictionaries.aspx