Framework Madness!

And other adventures C# and asp.net …

One Simple Wish for Asp.Net MVC 4

with one comment

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.

Written by Lynn Eriksen

June 2, 2011 at 11:23 pm

Posted in Uncategorized

“Here we go Mr. [Windows 8] …”

leave a comment »

Written by Lynn Eriksen

May 23, 2011 at 5:52 pm

Posted in Uncategorized

Using RazorEngine inside ASP.net MVC views

leave a comment »

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:

Code Snippet
  1. @using RazorEngine;
  2. @{
  3.     var Template = "Template Action: @Model.Action(\"Index\",\"Render\",new {Area=\"Testing\"})";
  4.  
  5.     Func<String, String, Object, MvcHtmlString> Action = (action, controller, values) =>
  6.                                                              {
  7.                                                                  return Html.Action(action, controller, values);
  8.                                                              };}
  9. @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.

Written by Lynn Eriksen

April 20, 2011 at 8:24 pm

Posted in Uncategorized

MvcScaffolding for Mvc 3 will Change Your Life as much as jQuery did. … … Seriously.

with one comment

More to follow.

Written by Lynn Eriksen

March 26, 2011 at 6:19 am

Posted in Uncategorized

Beyond the Propaganda: Microsoft Opens Html 5 labs

leave a comment »

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

Written by Lynn Eriksen

December 24, 2010 at 12:52 am

Posted in Uncategorized

Oh Yeah! Resharper 6 Early Access Program

leave a comment »

Written by Lynn Eriksen

December 24, 2010 at 12:43 am

Posted in Uncategorized

IE 9: The “Diet Coke” of Html 5 Browsers so far?

with one comment

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.

Written by Lynn Eriksen

September 24, 2010 at 12:02 am

Posted in Uncategorized

iTunes 10 is trash

with one comment

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.

Written by Lynn Eriksen

September 2, 2010 at 12:10 am

Posted in Uncategorized

Asp.net MVC 2: Creating a SimpleValuesModelBinder

leave a comment »

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:

Code Snippet
//this allows for supplying values using a simple key/value pair collection
    ////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:

Code Snippet
public class TestModel
    {
        public string Value { get; set; }

        public DateTime Current { get; set; }
    }

… and here is custom binder using the abstract class above:

Code Snippet
public class TestModelBinder : SimpleValuesModelBinder
    {

        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.

Code Snippet
public ActionResult Index([ModelBinder(typeof(TestModelBinder))]TestModel Test)
        {
            return Content(string.Format("Value: {0}; Current: {1}",Test.Value,Test.Current));
        }

 

And the the less than glamorous result:

image

So, if your looking to do some simple value parsing this may help.

Written by Lynn Eriksen

August 22, 2010 at 4:16 am

Posted in Uncategorized

Tagged with

IE 9 Preview 4 – How much HTML 5 support? Should it?

with one comment

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?

image

That’s right – 96  out of 300? You say you want details? Here you go:

 

Related specifications

 

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.

Written by Lynn Eriksen

August 5, 2010 at 10:09 am

Posted in Uncategorized

Tagged with