Archive for category asp.net
How To Reset Your SubSonic 3 ActiveRecord Test Repository For Each Unit Test
When writing unit tests with SubSonic’s new built-in testing for ActiveRecord don’t forget that each time you call the Setup() method on your ActiveRecord object it adds records to any that have already been created. For example:
[Test]
public void Foo_Bar() {
Foo.Setup(10);
Assert.AreEqual(10, Foo.All().Count());
}
[Test]
public void Bar_Foo() {
Foo.Setup(10);
// This will fail: Foo.All().Count() will return 20
Assert.AreEqual(10, Foo.All().Count());
}
To reset your test repositories you will need to call the ResetTestRepo() method prior to each test, which can be accomplished easily enough by adding the method call to your test’s [SetUp] method:
[SetUp]
public void SetUp() {
Foo.ResetTestRepo();
}
[Test]
public void Foo_Bar() {
Foo.Setup(10);
Assert.AreEqual(10, Foo.All().Count());
}
[Test]
public void Bar_Foo() {
Foo.Setup(10);
// Now Foo.All().Count() will return 10 and the test will pass
Assert.AreEqual(10, Foo.All().Count());
}
Hopefully this will save you a bit of time, as well as head scratching :p
C# String Extension Methods
There are quite a few instances where string operations in C# seem slightly verbose. So, I had a play around with extension methods the other day to see if I could come up with some alternate ways of doing some pretty standard operations but with a touch more syntactic sugar sprinkled on the top.
For example, rather than using string.IsNullOrEmpty it makes much more sense to me to call IsNullOrEmpty on the string you want to check instead:
// Checking for a null or empty string in C#
if (string.IsNullOrEmpty(foo)) {
...
}
// versus
if (foo.IsNullOrEmpty()) {
...
}
Also, I thought it would be nicer if you could call some of the static Regex methods directly on string objects:
// Instead of this
if (Regex.IsMatch(foo, @"[a-zA-Z]+")) {
...
}
// You could just do this
if (foo.IsMatch(@"[a-zA-Z]+")) {
...
}
I have created a gist of these methods, plus a few others:
Are there any other string operations that you think should be included? If you have any suggestions, etc. please be sure to comment.
Programmatically Getting the Site Root of an ASP.NET Web Application
If you ever find yourself needing to obtain the site root of your ASP.NET application you will hopefully find the following code useful:
public string SiteRoot {
get {
HttpRequest Request = HttpContext.Current.Request;
string appPath = Request.ApplicationPath;
if (appPath.Length > 1) {
appPath += "/";
}
string portNum = string.Empty;
if(Request.Url.Port != 80) {
portNum = string.Format(":{0}", Request.Url.Port);
}
return Request.Url.Scheme +
"://" +
Request.Url.Host +
portNum +
appPath;
}
}
On my last project it proved particularly useful as we could test it on our internal development servers before sending it across to the client; they themselves then ran the site on testing servers before deploying it onto their production server. All this was done without having to modify a single line of code or any configuration files.
If you are using ASP.NET 3.5 you could perhaps take this a stage further and implement this code as an extension method on, for example, the HttpRequest object.
If you have any suggestions on how this can be improved please let me know.
Adding Custom Attributes to ASP.NET Site Map Files to Improve Accessibility
Posted by Ian in accessibility, asp.net on June 7th, 2008
The Web.sitemap file was introduced in ASP.NET 2.0 and provides a really easy way to bind data to menus in your site. For example, given a Web.sitemap file containing this:
<sitemap
xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<sitemapnode url="Default.aspx" title="Home"
description="">
<sitemapnode url="about.aspx" title="About"
description="">
<sitemapnode url="contact.aspx" title="Contact"
description="">
</sitemapnode>
</sitemapnode>
We can bind this to a Repeater control using a SiteMapDataSource control to generate a list of links like so:
<ul>
<asp:SiteMapDataSource ID="siteMapData" runat="server"
ShowStartingNode="false" />
<asp:Repeater ID="menu" runat="server"
DataSourceID="siteMapData">
<ItemTemplate>
<li>
<a href="<%# Eval("url") %>"><%# Eval("title") %></a>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
This, unsurprisingly, renders the following HTML:
<ul>
<li>
<a href="about.aspx">About</a>
</li>
<li>
<a href="contact.aspx">Contact</a>
</li>
</ul>
Nothing you probably didn’t know already. But to make our menu more accessible, we can utilise the empty description attribute that Visual Studio provides us with by default to add some text to render as a HTML title attribute on each link:
<sitemap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<sitemapnode url="Default.aspx"
title="Home" description="Return to homepage">
<sitemapnode url="about.aspx"
title="About" description="About company XYZ">
<sitemapnode url="contact.aspx" title="Contact"
description="How to get in touch with us">
</sitemapnode>
</sitemapnode>
Now we just need to tweak our code slightly:
<ul>
<asp:SiteMapDataSource ID="siteMapData" runat="server"
ShowStartingNode="false" />
<asp:Repeater ID="menu" runat="server"
DataSourceID="siteMapData">
<ItemTemplate>
<li>
<a href="<%# Eval("url") %>" title="<%# Eval("description") %>">
<%# Eval("title") %></a></li>
</ItemTemplate>
</asp:Repeater>
</ul>
Which will render:
<ul>
<li>
<a title="About company XYZ" href="about.aspx">About</a>
</li>
<li>
<a title="How to get in touch with us" href="contact.aspx">Contact</a>
</li>
</ul>
This will show the description text as a title when you mouseover each link. But we can take this a step further as the ASP.NET site map files support custom attributes. Lets add an access key so that users can navigate using their keyboard:
<sitemap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<sitemapnode url="Default.aspx"
title="Home" description="Return to homepage" accesskey="H">
<sitemapnode url="about.aspx"
title="About" description="About company XYZ" accesskey="A">
<sitemapnode url="contact.aspx" title="Contact"
description="How to get in touch with us" accesskey="C">
</sitemapnode>
</sitemapnode>
Custom attributes are accessed via an indexer e.g.
// node is a SiteMapNode instance string innerText = node.Title; // accesses the Title property string shortcut = node["accessKey"]; // accesses the accessKey custom attribute
We can add this to our markup like this:
<ul>
<asp:SiteMapDataSource ID="siteMapData" runat="server"
ShowStartingNode="false" />
<asp:Repeater ID="menu" runat="server"
DataSourceID="siteMapData">
<ItemTemplate>
<li>
<a href="<%# Eval("url") %>" title="<%# Eval("description") %>"
accesskey="<%# Eval("[accessKey]") %>">
<%# Eval("title") %></a>
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
This will render the following HTML:
<ul>
<li>
<a accesskey="A" title="About company XYZ" href="about.aspx">About</a>
</li>
<li>
<a accesskey="C" title="How to get in touch with us" href="contact.aspx">Contact</a>
</li>
</ul>
If you fancy playing around with the above sample code please feel free to download it.
Hope you find this useful.
Data Binding to Key-Value Based Collections in ASP.NET
This is as much a note for me as anything as every time I need to data-bind a control to a Hashtable or a Dictionary<TKey, TValue> I always find myself reaching for Google.
When data-binding to a key-value based collection you output data as follows:
-
<%# Eval("Key") %> or
-
<%# Eval("Value") %>
Simple really.