ASP.NET MVC Gravatar HtmlHelper
Posted: September 30, 2009 Filed under: ASP.NET MVC, Code | Tags: ASP.NET MVC, Extension Methods, Gravatar 1 Comment »Here is a quick little URL Helper extension method to add Gravatar images/photos/avatars to your page:
public static string GravatarUrl(this UrlHelper url, string email, int size)
{
string imageUrl = ConfigurationManager.AppSettings["DefaultGravatar"];
if (imageUrl.StartsWith("~/"))
{
imageUrl = url.Absolute(imageUrl);
}
if (string.IsNullOrEmpty(email))
{
return imageUrl;
}
string md5 = email.ToLowerInvariant().MD5();
return string.Format(
"http://www.gravatar.com/avatar/{0}.jpg?d={1}&s={2}&r=g",
md5.ToLowerInvariant(),
url.Encode(imageUrl),
size);
}
public static string GravatarUrl(this UrlHelper url, string email)
{
return url.GravatarUrl(email, 32);
}
It uses another custom URL Helper extension method Absolute to generate an absolute URL to a default image (based on an application relative url).
The Absolute extension method is similar to the ActionAbsolute method I posted earlier:
public static string Absolute(this UrlHelper url, string contentUrl)
{
return new Uri(GetBaseUrl(url), url.Content(contentUrl))
.AbsoluteUri;
}
And it also used the MD5 extension method from yesterdays post.
Getting an absolute URL from ASP.NET MVC
Posted: January 13, 2009 Filed under: ASP.NET MVC | Tags: ASP.NET, ASP.NET MVC, url 4 Comments »I have been in situations where I need to generate an absolute URL to a specific action in an ASP.NET MVC application. The main problem mostly lies in getting the “domain” part of the URL. The only way to do this is to examine the request URL. I came across a very nice method of doing this during an encounter with the dotnetopenid.
public static Uri GetBaseUrl(this UrlHelper url)
{
Uri contextUri = new Uri(url.RequestContext.HttpContext.Request.Url, url.RequestContext.HttpContext.Request.RawUrl);
UriBuilder realmUri = new UriBuilder(contextUri) { Path = url.RequestContext.HttpContext.Request.ApplicationPath, Query = null, Fragment = null };
return realmUri.Uri;
}
public static string ActionAbsolute(this UrlHelper url, string actionName, string controllerName)
{
return new Uri(GetBaseUrl(url), url.Action(actionName, controllerName)).AbsoluteUri;
}
jQuery and AJAX Tooltips
Posted: January 1, 2009 Filed under: ASP.NET MVC, jQuery | Tags: javascript jquery tooltip 2 Comments »So the day has dawned, very symbolic that this day happens to be the first of a new year… enough of that. The day has finally come where I post a JavaScript entry! Who would have thought that???
Over the last couple of months I have had a very passionate relationship with a new hottie I discovered. She is named jQuery and OMG can she do tricks… OK, stop this talk right now
Basically I have had my head in AJAX for ASP.NET doing what others before already realized probably wasn’t the best approach. Finally I got around to have a look at jQuery and I must admit that (especially for my ASP.NET MVC project(s)) I haven’t looked back!!!
Today I had a little feature I wanted to solve with some AJAX:
I have a list of objects displayed in a certain view. One property on these objects is calculated and involved some semi-heavy database and calculation stuff and therefore requires both time and system resources to get. This value is not in particular important to the current view, but is a nice-to-have information. This means that the value as such does not need to appear in the view after rendering. I therefore figured it could be nice to have in a dynamic tooltip, i.e. “on tooltip hover, calculate/retrieve the value and display it as a tooltip”.
Having used other of Jörn Zafferer jQuery plug-in’s previously (with great success) I turned my attention to his tooltip plugin.
Having read the documentation it is apparent that, while you can specify a callback function (namely the “bodyHandler” property) to determine which “value” to display in the tooltip, there is an issue with this: it is synchronous! And by definition AJAX is asynchronous. The problem here is that if you do a $.get() call you the return value of the bodyHandler callback is “out of scope”, since it is first available once the AJAX call is completed, i.e.:
$(".tooltip-element").tooltip({
bodyHandler: function() {
$.get("/MyController/MyAction", { id : "myid" }, function(data) { /* do what with "data"? */; }, "html");
},
showURL: false
});
My solution to this was the following:
$(".tooltip-element").tooltip({
bodyHandler: function() {
var result = $("<span/>").html("some loading indicator");
$.get("/MyController/MyAction", { id: "myid" }, function(data) { result.html(data); }, "html");
return result;
},
showURL: false
});
I think that is pretty nifty! Of course the “some loading indicator” can be tweaked (e.g. use a AJAX load indicator). And this is of course not specific to ASP.NET MVC!
UPDATE:
There is a problem with this approach! The jQuery tooltip plugin used seems to invoke the bodyHandler callback directly “on mouse over”, ie. not after the delay. This means that although the tooltip displays nicely after the specified delay, the Ajax call was performed at T=0. What this effectively means is that if you eg. have a table where all values in column 1 have this Ajax-tooltip-thingy. If you then move the mouse in over a couple of the values each time you the cursor is over the value an Ajax call is executed. Not so good unless you want to utilize all resources in your server
Back to the drawing board…
ASP.NET MVC HtmlHelper methods and CSS class
Posted: November 19, 2008 Filed under: ASP.NET MVC | Tags: ASP.NET, ASP.NET MVC, reference 4 Comments »If you in ASP.NET MVC use a Html helper method to render out e.g. an input box:
<%= Html.TextBox("name") %>
Sometimes you might need to add an CSS class attribute to your input box (this should be a fairly standard thing). For this there exist overload methods that takes a “htmlAttributes” parameter of type object. Now my experience with this is somewhat similar to Ayende Rahien experience – although I have not begun banging my head against the wall out of frustration. This is not a rant, I love ASP.NET MVC, I just had an experience with something simple that was hard to figure out because of some of the design decisions – not that they are bad, they can just be confusing – especially with the lack of documentation at the current stage (yes, I know it is just a beta). So if you try this:
<%= Html.TextBox("name", null, new { class = "required"}) %>
You will (of course) get a compiler error, since “class” is a keyword and cannot be used like this. So how do you specify the class? Well, like this:
<%= Html.TextBox("name", null, new { @class = "required"}) %>
Just for future reference