I’ve recently released several new open source projects on GitHub:
Home | Archives |
I’ve recently released several new open source projects on GitHub:
See this example
In short, when the object is treated as the interface the default value from the interface applies. If it’s treated as the implementation, the implementation’s default applies. But the implementation isn’t required to have a default for it at all - in which case the call will fail, unless it’s treated as the interface.
It’s all very unreliable, and while I’m sure it works this way for good technical reasons under the hood, realistically it doesn’t present a very coherent interface.
I was watching a presentation on HTML5 from one of my coworkers a few weeks ago and we were talking about the new input types in HTML5, like numbers and dates. They’re backward compatible with non-HTML5 browsers (which render them as text boxes), but provide very useful UI features particularly to smartphones where the on screen keyboard can change to be more appropriate.
Anyway, let’s just say they’re a good idea. A good idea that, much as I like ASP.NET, are not likely to make an appearance any time soon in the core framework. So, I looked around to see if I could figure out a way to patch a standard asp:TextBox to render these new HTML5 types. Yes, you MVC types already can do this easily. For the rest of us that are using third party CMS tools, WebForms is still the way of life :)
I came across a post by Phil Haack about a sneaky method of overriding HtmlTextWriter to change the attributes output by WebControls, and repurposed it to accomplish the HTML5-ization. Basically you override the AddAttribute() method and make it ignore the attribute you want to manually handle - in this case, the type attribute of the TextBox. Then I made a new TextMode-style enum that encompasses the HTML5 types and wrote a bit of logic that manually creates the right type attribute. The new enum is exposed in the Html5TextMode property, and the value is proxied back into the default TextMode property if it’s compatible with it.
So how do you use it? It’s pretty simple:
<prefix:Html5TextBox runat="server" Html5TextMode="Tel" ID="telephone" />
And the code for the control itself:
public class Html5TextBox : TextBox
{
/// <summary>
/// When using non-HTML5 constructs this mode will be accurate. If using HTML5, it will return SingleLine.
/// </summary>
public override TextBoxMode TextMode
{
get
{
try
{
return (TextBoxMode)Enum.Parse(typeof(TextBoxMode), Html5TextMode.ToString());
}
catch(ArgumentException) { return TextBoxMode.SingleLine; }
}
set
{
Html5TextMode = (Html5TextBoxMode)Enum.Parse(typeof(Html5TextBoxMode), value.ToString());
}
}
/// <summary>
/// Sets the text mode of the control including HTML5 text modes such as Num and DateTime
/// </summary>
public Html5TextBoxMode Html5TextMode
{
get
{
object textMode = this.ViewState["5Mode"];
if (textMode != null)
{
return (Html5TextBoxMode)textMode;
}
return Html5TextBoxMode.SingleLine;
}
set
{
this.ViewState["5Mode"] = value;
}
}
/// <remarks>
/// Adds the normal attributes (since the writer is actually a PatchedHtmlTextWriter from Render() the type attribute won't be rendered),
/// then explicitly adds the appropriate type attribute including the HTML5 extensions
/// </remarks>
protected override void AddAttributesToRender(HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
string type = null;
if (Html5TextMode == Html5TextBoxMode.SingleLine)
type = "text";
else if (Html5TextMode == Html5TextBoxMode.DateTimeLocal)
type = "datetime-local";
else type = Html5TextMode.ToString().ToLowerInvariant();
if (type != null)
(writer as PatchedHtmlTextWriter).AddTypeAttribute(type);
}
/// <remarks>
/// Patches the type of HtmlTextWriter that the control renders to
/// </remarks>
protected override void Render(HtmlTextWriter writer)
{
base.Render(new PatchedHtmlTextWriter(writer));
}
/// <summary>
/// A version of HtmlTextWriter that intentionally ignores the "type" attribute when it is added.
/// </summary>
/// <remarks>
/// Technique courtesy of Phil Haack
/// http://haacked.com/archive/2006/01/18/UsingaDecoratortoHookIntoAWebControlsRenderingforBetterXHTMLCompliance.aspx
/// </remarks>
private class PatchedHtmlTextWriter : HtmlTextWriter
{
internal PatchedHtmlTextWriter(HtmlTextWriter basis) : base(basis) { }
public override void AddAttribute(HtmlTextWriterAttribute key, string value)
{
if(key != HtmlTextWriterAttribute.Type)
base.AddAttribute(key, value);
}
public override void AddAttribute(string name, string value)
{
if(name != "type")
base.AddAttribute(name, value);
}
/// <summary>
/// Manually adds a type attribute
/// </summary>
public void AddTypeAttribute(string value)
{
base.AddAttribute(HtmlTextWriterAttribute.Type, value);
}
}
}
/// <summary>
/// Extends the TextMode enum with additional HTML5 types
/// </summary>
public enum Html5TextBoxMode { MultiLine, SingleLine, Password, DateTime, DateTimeLocal, Date, Month, Time, Week, Number, Range, Email, Url, Search, Tel, Color }
When running Sitecore in a multi-site configuration you may run into an odd issue: output caching may seem to get too greedy and not clear when you’d expect it to.
There’s a simple culprit: the default Sitecore setup includes an event handler, Sitecore.Publishing.HtmlCacheClearer
, that is invoked on the publish:end
event. This event handler has a list of sites assigned to it, and the default is “website” - great, until you need to have more than one site and publishing doesn’t clear your site’s output cache. Fortunately it’s easy to configure more sites: just add more site nodes to the XML. You cannot however use config includes to allow each site to individually add itself to the list from its own config file.
There’s also a nuclear option: you can implement your own event handler that clears all sites’ caches. I’m not sure if this would have a detrimental effect on any of the system sites (i.e. shell), but you could exclude it. An example of doing that:
string[] siteNames = Factory.GetSiteNames();
for (int i = 0; i < siteNames.Length; i++)
{
SiteInfo siteInfo = Factory.GetSiteInfo(siteNames[i]);
if (siteInfo != null)
{
siteInfo.HtmlCache.Clear();
}
}
Disclaimer: This was written about Visual Studio 2010 RC. I suspect the same approach will continue to work with the final release but there are no guarantees.
When I first heard about Web.config transformations in Visual Studio 2010 I was excited. Our build process involves keeping a working build in SVN such that our dev servers may merely check out a working copy and go, and the problem of people committing their own, or the dev server's, web.configs had been a problem. Someone else's settings would come to your machine, causing explosions and other hijinks. Wouldn't it be great, I thought, to have each developer keep a delta for their own web.config settings and apply them only on their box, so not only would the commit problem be solved, we could also keep everyone's personal configs safe in SVN.
My problem was that I assumed the transformations would apply more or less at runtime. Their dirty secret is that they're only applied when you publish the site. We don't use VS publishing ever, as our release process is entirely based around SVN tags. This also kills its utility for the situation of having multiple developers with disparate web.configs being able to maintain individual transformations that apply to their computer only.
Feeling disappointed, I set out to remedy the situation. As I knew this was all handled by MSBuild thanks to Hanselman's talk at MIX10, I went spelunking in the global targets files. I found the definition of the TransformWebConfig target, which I learned about from this blog post, in the C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets file. With a little fooling around, I began to understand how it works in its essential nature: the Microsoft.Web.Publishing.Tasks.TransformXml task (defined in an assembly in the same directory as the targets file).
Armed with the knowledge of how the task works by example and Reflector, I set out to try and make it do what I wanted: after a build, apply the correct transformation file and overwrite the Web.config. Since there had to be a source file other than the Web.config to transform on, I settled on "Web.generic.config" as a name for the untransformed config.
It turned out to be quite simple to implement. In my project's .csproj file, there lies at the bottom a commented out section with BeforeBuild and AfterBuild tasks already skeletoned out.
I uncommented the AfterBuild task and added the TransformXml task to it, like so:
<target name="AfterBuild">
<TransformXml Source="Web.generic.config"
Transform="$(ProjectConfigTransformFileName)"
Destination="Web.Config" />
</target>
Voila - after each build, publishing or no, the Web.generic.config has the active solution configuration's transformation applied to it and copied to the Web.config. Note that unlike the version run during publishing, this one only checks the root Web.config. Subdirectories' config files are ignored. Someone better than I with MSBuild could probably remedy that shortcoming :)
I suspect that with the right importation of tasks into your project file, this solution could be made to work against regular old App.config files as well as Web projects, but I don't do enough app development to dive particularly deeply into it.
Visual Studio 2010 brings about a bunch of new "coder-centric" features, some of which I'm practically drooling over. Post based on a session with Jeff King, Program Manager of Visual Studio Web Tools. He's a cool guy.
ASP.NET 4.0 is going to introduce a lot of pain-point reducing features, particularly around the efficient delivery of content and standards compliance. Here are the main points as I saw them in Stephen Walther's talk this morning.
Bill Buxton of MS Research led off, looking quite like the professor in Back to the Future. The key to UX, he says, is to balance budget with multiples - multiple ways of accomplishing the same task, that can be chosen between. "The transitions are just as important as the states," he says. Better transition documentation would end up with a better UX implementation of said process in the final product. "Ideation," basically brainstorming, actually lowers usability unless followed by a stage of reflection/iteration on the ideas created.
Expression web 3 is coming. SuperPreview feature can load previews using different browser engines, including Firefox and Safari from within the Expression interface, as well as a side-by-side view with the original comp or overlaying the comp as a transparency. It can also get previews for things like Safari Mac by using a MS cloud service to provide the rendering. It can also firebug-style outline elements while n side-by-side view to compare browsers (ftw!). Free version of SuperPreview only available now, which enables side by side display of multiple versions of IE with no VMs, etc. More on SuperPreview later, after I condense my notes from discussing it with one of the developers on the project.
ASP.NET MVC 1.0 RTM is released.
Some features of Visual Studio 2010 were discussed but I will discuss those in my post about the VS session I was at later in the day.
IIS 7's FTP module getting an update including SFTP support. Much needed, IIS FTP has been pretty terrible for ages now.
Web Platform Installer 2.0 coming out, available from http://microsoft.com/web, which extends on the 1.0 featureset of being able to configure IIS and install Microsoft development tools (SQL/VS express, IIS 7 extensions like URL Rewrite) by adding an "app store for your web server" as ScottGu put it. I think that sounds like a great idea. The Web PI app gallery has an open interface and many common products are already on it - even not traditionally Microsoft apps like PHP and Drupal, as well as open source .NET apps like DotNetNuke and Umbraco.
Silverlight 3 beta is released today. Many many new features to compete with Flash and AIR.
That's all folks. Some exciting stuff coming down the pipe here, with more details in some entries I need to write from my sessions today.
Well I'll be getting on the plane to go to MIX09 in a few hours. I'm really looking forward to it, since there are a lot of ASP.NET MVC talks this year by many .NET people who I have a lot of respect for.
Since I do a lot of CMS development ASP.NET MVC has always a bit of an odd proposition technically. CMSes pretty much always want to enforce their own idea of how to program, which tends to be technically about as advanced as putting SQL statements right on the page so their marketing department can make claims about how easy it is to use if you require practically no advanced functionality. It's a constant battle to figure out ways of writing maintainable, "good" code within the confines of a CMS's API, and ASP.NET MVC is definitely one of the tools I'm looking at.
The main problem with integrating ASP.NET MVC into a CMS installation is that almost all CMS like to have a fully hierarchical URL setup; i.e. http://mysite.com/some/long/path/to/rewritten/content.aspx that would match /some/long/path... in the admin interface. The default routing system from what I've been looking at won't really support this setup since what we're really doing is defining what item in the (dynamically created and modifiable) hierarchy to look at as opposed to a (static, defined in code) controller action to invoke. So I think it's likely I'd need to implement my own version of routing, but that brings with it it's own issue that the custom routing then needs to know a lot about the data before passing control off to a controller action to render a page using a specific template renderer (based on the type of page it finds at the location in the hierarchy). I'm still looking around and hopefully can corner one of the MVC guys at MIX and see if they have better ideas :)