No more than my online scratch pad.
Handling Google Instant in ASP MVC
I love google chrome, it’s how browsers should be, it just works. The one thing I really like about it is the instant setting. The problem is that most sites don’t really know the difference between a bad url and a url that is being typed.
Well, now you can and it’s really simple.
The following extension method will let your controller know if it’s a full request or simply a user busy typing:
public static class RequestExtensions{ public static bool IsPreview(this HttpRequestBase request) { var purpose = request.Headers["X-Purpose"] ?? string.Empty; return string.Equals(": preview", purpose, StringComparison.InvariantCultureIgnoreCase); }}A very simple use of it would be something like this:
public virtual ActionResult Error404(){ Response.StatusCode = (int)HttpStatusCode.NotFound; if (Request.IsPreview()) { ViewBag.Message = "Nope, I've haven't go that, keep typing."; } else { ViewBag.Message = "Sorry, but the file you requested was not found on the server."; }
return View();}Let me know if you use this is a more creative way.