One Simple Wish for Asp.Net MVC 4
One feature that is not widely known is the ability to edit your MVC Project file and turn on the ability to build MVC views at compilation time. This really helps with debugging, and this works nicely if you have a small project. But if you have a large project builds can drag on forever. So, one thing I would like is a ‘build views’ command so I can build as necessary.
Using RazorEngine inside ASP.net MVC views
Recently while searching for a way to do runtime Razor parsing I found the RazorEngine at http://razorengine.codeplex.com/. It’s pretty simple – it allows you parse Razor syntax template strings on the fly and render them. However, support for using with MVC View is fairly limited. However, with a bit of simple creativity you can get a long way.
Here is a code sample:
- @using RazorEngine;
- @{
- var Template = "Template Action: @Model.Action(\"Index\",\"Render\",new {Area=\"Testing\"})";
- Func<String, String, Object, MvcHtmlString> Action = (action, controller, values) =>
- {
- return Html.Action(action, controller, values);
- };}
- @Razor.Parse(Template, new { Action = Action })
Here are the main points:
- The RazorEngine is accessed by calling the “Razor” static class as seen on line 9.
- The string template is created in line 3.
- To get output, we pass a string template, plus a model, to “Razor.Parse” which produces the output.
- In this case we are passing an anonymous object with a property “Action”, which is the Func delegate on starting on line 5.
The reason we’re passing a delegate here to call the Html.Action method instead of passing the Html object itself is that we cannot get extensions method support easily with the RazorEngine parser.
- When parsed and executed, the template calls the Action delegate that is on the Model object available to it. This call then executes Html.Action and returns the string output, which is then in-lined with the remainder of the template.
The results are simple:
Template Action: (Render: 56844b67-a1ea-4184-9daf-79036778467a)
Everything after “Template Action:” comes from the child action which is parsed from the string template and executed .
My ultimate goal would be to do runtime parsing that has the same affect as using the ShortCodes api in WordPress.
MvcScaffolding for Mvc 3 will Change Your Life as much as jQuery did. … … Seriously.
More to follow.
Beyond the Propaganda: Microsoft Opens Html 5 labs
It’s good to have an open discussion on how HTML 5 is really an incomplete moving target … and Microsoft’s plans for that.
http://blogs.msdn.com/b/ie/archive/2010/12/21/html5-site-ready-and-experimental.aspx
IE 9: The “Diet Coke” of Html 5 Browsers so far?
IE 9 Beta Has No Love for Data Developers
I am in general concerned about the what directions in Html 5 Microsoft has not taken in IE 9. Those it has, I am really enjoying. If you want to see how IE 9 beta scores on a basic HTML 5 readiness tool, kit here: http://html5test.com/.
At heart I am a data guy, and that means I have a strong interest in files and forms. And here is where IE 9 comes up short in regard to HTML 5. Let’s look at the highlights of what’s missing as reported by taking your IE 9 beta the link above.
- No new input types, attributes, elements or validation.
Want out-of-the-box multiple file upload or date picker? Thus far – your out of luck. And there is a lot more missing than that.
- No File API.
Not heard of this? This allows your to declare an element on the page as a drop source for files and allows you to send the files to the server via ajax. Mozilla and WebKit have this.
- No IndexedDB
Microsoft and Mozilla have successfully championed that JSON-based data storage service instead of Web SQL, but it is curiously absent from the beta.
There are others, but these are most vital for data developers. Microsoft, if you really want to make IE 9 be a win for data developers you need to implement these in this version of the product even if you have to delay the product a few months. Even if that means trying to work thru the W3C to get these finalized early, and/or partial implementation.
Given the rate of adoption of IE, the frequency of releases by Microsoft, and that adoption will be initially slowed due to a lack of XP support, it would be easily 3 years from now before we can consider broad support for these features to be available if they are included in this version. If these features have to wait for IE 10 then I am afraid it will be too late.
If you are a data developer and agree, let Microsoft know your concerns have not been addressed. I have said nothing until now, because I foolishly assumed they would show up in the beta. I was wrong.
BTW – please consider adding the Web Feed notification icon back to the icons list next to the home button. A LOT of people have been to look for the icon in their browser, and may panic when it gone. I have over 150 feeds I read with IE, and I panicked when I couldn’t find it.
I am a web developer and use IE 9 beta as my primary browser at home.
iTunes 10 is trash
Downloaded the 64 bit edition on Win 7.
First – it didn’t recognize my previous install and had to authenticate the machine. I already had one gone due to a previous “Oh, I forgot to deauthorize”. That’s two gone now, for nothing.
The new movie player plays well and is slow to respond. Starting a movie up takes FOREVER. Then I was trying to scroll back on the movie when the sound track just goes away. Gone. No sound. My TV movies have sound, but no more sound on that movie I just rented.
So I’m going back to 9.2. Hopefully Apple will get v10 right in a few months. We’ll see.
Asp.net MVC 2: Creating a SimpleValuesModelBinder
I have been thinking about what it would take to make a custom model binder for the PayPal IPN service the past week or so, and tonight I finally had the chance to look into it. The goals is simple – I just want to pass the IPN form values to a model that has properties with different names. I don’t want to do any custom model binding, perhaps a small bit of string parsing but that’s about it.
I looked into model binding at the MSDN library, Steve Sanderson’s book, and the TekPub MVC series, they all were good, but what I wanted to do was simple. On thing I did get out of it was that I would need both a custom ModelBinder implementing IModelBinder and possibly a custom ValuesProvider as well.
Finally, I looked into the MVC source for the DefaultModelBinder to see how it worked and came up with pure gold. Here is the basic premise of the solution:
- Create an abstract model binder class that uses the DefaultModelBinder class to do its work.
- Leverage the NameValueCollectionValueProvider class to provide simple Key/Value pairs to the DefaultModelBinder .
- Create a custom copy of the supplied ModelBindingContext that connects the binder and provider together.
Here’s the code with comments:
////all complicated binding set up is abstracted
//DefaultModelBinder is used for ModelBinding
//NameValueCollectionValueProvider is used as the ValueProvider
public abstract class SimpleValuesModelBinder : IModelBinder
{
protected abstract void AppendValues(ControllerContext controllerContext, NameValueCollection collection);
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
//first we want to call AppendValues
//the collection is supplied to the SimpleValuesProvider
//we use the NameValueCollectionValueProvider because it uses key/value pairs
var collection = new NameValueCollection();
AppendValues(controllerContext, collection);
//create a custom binding context
//we pass thru properties except the value provider
//we use the new TestValueProvider here
var customContext = new ModelBindingContext()
{
ModelMetadata = bindingContext.ModelMetadata,
ModelState = bindingContext.ModelState,
PropertyFilter = bindingContext.PropertyFilter,
ValueProvider = new NameValueCollectionValueProvider(collection, CultureInfo.CurrentCulture)
};
//pass our custom binding context the default binding model and bind
return new DefaultModelBinder().BindModel(controllerContext, customContext);
}
}
Creating a custom model binder from this class is very simple. Here is a simple class:
{
public string Value { get; set; }
public DateTime Current { get; set; }
}
… and here is custom binder using the abstract class above:
{
protected override void AppendValues(ControllerContext controllerContext, NameValueCollection collection)
{
collection["Value"] = Guid.NewGuid().ToString();
collection["Current"] = DateTime.Now.ToString();
}
}
The resulting custom model binder is very simple. All you have to do is override one method, AppendValues, and your done. In this case I am supplying arbitrary values as a test, but you can leverage any values you want from the ControllerContext parameter.
Of course set up is easy too, ASP.net MVC allows several ways to do this. For a test, I’ll just apply to a parameter in an action method.
{
return Content(string.Format("Value: {0}; Current: {1}",Test.Value,Test.Current));
}
And the the less than glamorous result:
So, if your looking to do some simple value parsing this may help.
IE 9 Preview 4 – How much HTML 5 support? Should it?
I’ve been wondering how to get thru the hype of Html 5 support in IE 9. That’s not to say that the improvements they have made aren’t welcome – they very much are. But Html 5 is larger spec than just audio, video and canvas – and I’ve been wondering how broad Html 5 support goes.
I found a site called http://html5test.com/ that will test your browser for HTML 5 support. How does IE 9 preview 4 fare?
That’s right – 96 out of 300? You say you want details? Here you go:
- Parsing rules1 Bonus point1/11
<!DOCTYPE html>
triggers standards mode
Yes ✔HTML5 tokenizer
No ✘HTML5 tree building
No ✘HTML5 defines rules for embedding SVG and MathML inside a regular HTML document. Support for SVG and MathML is not required though, so bonus points are awarded if your browser supports embedding these two technologies.
SVG in
text/html
Yes ✔MathML in
text/html
No ✘ - Canvas20
canvas
element
Yes ✔2D context
Yes ✔Text
Yes ✔ - Video22/27
video
element
Yes ✔Subtitle support
No ✘Poster image support
Yes ✔The following tests go beyond the requirements of the HTML5 specification and are not counted towards the total score. If a browser supports one or more video codecs, two bonus points are awarded for each codec.
MPEG-4 support
No ✘H.264 support
No ✘Ogg Theora support
No ✘WebM support
No ✘ - Audio2 Bonus points20
audio
element
Yes ✔The following tests go beyond the requirements of the HTML5 specification and are not counted towards the total score. If a browser supports one or more audio codecs, one bonus point is awarded for each codec.
PCM audio support
No ✘MP3 support
Yes ✔AAC support
Yes ✔Ogg Vorbis support
No ✘WebM support
No ✘ - Local devices0/20
device
element
No ✘ - Elements13/30
Embedding custom non-visible data
No ✘Section elements
No ✘section
element
No ✘nav
element
No ✘article
element
No ✘aside
element
No ✘hgroup
element
No ✘header
element
No ✘footer
element
No ✘Grouping content elements
No ✘figure
element
No ✘figcaption
element
No ✘Text-level semantic elements
2 out of 5mark
element
No ✘ruby
element
Yes ✔rt
element
Yes ✔rp
element
No ✘time
element
No ✘hidden
attribute
No ✘Scroll into view
Yes ✔ - Forms0/38
input
element types
No ✘search
type state
No ✘tel
type state
No ✘url
type state
No ✘email
type state
No ✘datetime
type state
No ✘date
type state
No ✘month
type state
No ✘week
type state
No ✘time
type state
No ✘datetime-local
type state
No ✘number
type state
No ✘range
type state
No ✘color
type state
No ✘input
element attributes
No ✘autofocus
attribute
No ✘list
attribute
No ✘max
attribute
No ✘min
attribute
No ✘multiple
attribute
No ✘pattern
attribute
No ✘required
attribute
No ✘step
attribute
No ✘Other form elements
No ✘datalist
element
No ✘keygen
element
No ✘output
element
No ✘progress
element
No ✘meter
element
No ✘Form validation
No ✘ - User interaction5/25
Drag and drop
No ✘Undo history
No ✘Session history
No ✘Text selection
Yes ✔ - Microdata0/10
Microdata
No ✘ - Web applications0/14
Application Cache
No ✘
Related specifications
- Geolocation0/10
Geolocation
No ✘ - WebGL0/10
3D context
No ✘ - Communication5/25
Cross-document messaging
Yes ✔WebSocket
No ✘Server-Sent Events
No ✘ - Files0/10
FileReader API
No ✘ - Storage10/20
Session Storage
Yes ✔Local Storage
Yes ✔IndexedDB
No ✘The Web SQL Database specification is no longer being updated and has been replaced by IndexedDB. Because at least 3 vendors have shipped implementations of this specification we still include it in this test.
Web SQL Database
No ✘ - Workers0/10
Web Workers
No ✘
Clearly, while the IE team has done great work in bring IE forward with the IE 9 previews, clearly there is a lot more HTML left unsupported then currently in the latest preview. But should IE 9 have full HTML 5 support? Good question. If you take a look at the HTML 5 document at the W3C you’ll notice it’s “working draft” – the proposal in still flux. I would guess that MS is just expanding “de facto” standards support and hitting the highlights, where other features such as IndexDB, Web Applications, Html 5 Forms and other elements will come – eventually. But will they come in IE 9? Only MS knows the answer to that one. Let’s just hope we don’t have to wait three years until IE 10 to find out.