Framework Madness!

And other adventures C# and asp.net …

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.

Advertisements

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

First Asp.net MVC 3 Details Emerge: Scott Guthrie Announces “Razor” View Engine

leave a comment »

Looks like a MUCH BETTER editing experience is right around the corner (and maybe a few blocks away) .

http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx?

Written by Lynn Eriksen

July 3, 2010 at 1:19 am

Posted in Uncategorized

Global AJAX Request Error Display for Asp.net MVC 2

leave a comment »

 

So you thought you might like to,
Go to the show.
To feel the warm thrill of confusion,
That space cadet glow.
Tell me is something eluding you, sunshine?
Is this not what you expected to see?

 

Ever get that kind of feeling when trying to debug server side request errors using AJAX (MS AJAX or jQuery) in MVC. I mean, on a normal posts that fails you get the nice yellow error screen that says “Hey you …” and “Bring on the (Stack Trace)!” and makes your wonder “Is there anybody out there?”. We’ll no more. Here’s a basic script you can just hook up while building/debugging your app that if you get a server side error making an AJAX request you’ll get the same effect as a full post back.

 

Code Snippet
/// <reference path="../Common/jquery-1.4.1-vsdoc.js" />
/// <reference path="../Common/MicrosoftAjax.js" />

$(document).ready(function () {

    //replace the screen with the response error

    //jQuery —————————————————–
    $(document).ajaxError(function (event, request, settings) {

        $(document.body).html(request.responseText);

    });

    //MS AJAX —————————————————-
    function OnWebRequestComplete(executor, eventArgs) {

        if (executor.get_statusCode() === 500) {

            $(document.body).html(executor.get_responseData());
        }
    }

    Sys.Net.WebRequestManager.add_completedRequest(OnWebRequestComplete);

});

Written by Lynn Eriksen

March 30, 2010 at 12:13 am

Posted in Uncategorized

Tagged with ,

Inside Cell Networks

leave a comment »

Written by Lynn Eriksen

March 29, 2010 at 6:07 am

Posted in Uncategorized

Strong-Typed Routes with Asp.net MVC 2 Preview 2

with one comment

Update 3/28/2010

Didn’t realize it had been so long since I have created this post. It appears that the MVC Futures team has stepped up and published a set of strong-typed route extensions in recent build. You can find a link to these on the MVC Page on CodePlex.

I’ll be publishing a set of extensions I have made to work with these in a few days based on ideas in the work below. Therefore, I am removing the download link.

 


Last weekend I enthusiastically went on and on about using Strong-Typed routes in MVC 2 Preview 2 based on Goran Gagic’s elegant sample. And so I have built on his code and I have something to share.

Download Sample Project Here

I am going to dive more into usage than working internals, so let’s get underway.

Project Configuration

I have made some configuration changes to the sample project to better illustrate the feature set and make usage very convenient when constructing views. First, I have added a few namespaces to the web.config as you can see in the image below:

strong-typed-routes web-config namespaces

The namespace ‘MVC2_TypedRoutes.TypedRoutes’ (the ‘TypedRoutes’ folder) contains the extensions and support classes for strong-typed  route value creation. Secondly, I have added an additional routes in the global.asax as show below:

strong-typed-routes global-asax extra-routes

Both of these routes utilize the TypedRoutesController exclusively, but differ in their signature using the ‘_RouteName’ value. I have also set up a simple controller that will be used to demonstrate the features. Let’s look at the controller:

strong-typed-routes typed-routes-controller

There are several attributes applied here that are leveraged by the strong-typed route extensions. Let’s review them:

  • RouteActionAttribute

    This attribute has a single property named ‘PassThru’ that allows for a entering a comma separated list of route value names to be passed thru when creating routes. This attribute can be applied to a class or method.

    In the sample it is applied to the class, so all strong-typed routes created for the TypedRoutesController will have the ‘_RouteName’ route values passed along when generating a route call.

  • RouteValueAttribute

    This attribute allows for specifying a format for the values in the route (especially handy for dates), default values and if the parameter is ignored in creating the route. It can be applied to parameters or properties.

    In the sample as applied to the ‘Date’ parameter in the ‘Sample1’ action method it specifies a default DatetTime of Jan 1, 2000. If the date value submitted when creating the strong-typed route in an Html.ActionLink<Controller> extension matches the default, it will not be passed to the resulting route. Additionally, the ‘Format’ value is specified and a route friendly format for the date parameter will be used.

Additionally, the ‘Sample2’ action method has the MVC BindAttribute applied and this will be used by the strong-typed route extensions as well.

Project Usage

The sample get’s right to the point, creating three strong-typed routes inside the HomeController ‘Index’ view to the TypedRoutesController. Let’s take a look at the ActionLink<Controller> calls and the resulting routes.

Sample ActionLink Route 1

strong-typed-routes sample-1

Here’s the run down on using the strong-typed ActionLink:

  • It’s a generic method – so you have to specify the controller
  • The first parameter is simple – the title for the action link
  • The second parameter is an expression based call to a method on the TypedRoutesController, in this case ‘Sample1’. (Notice the date supplied is the same as the default specified by the RouteValueAttribute.)
  • The third parameter is something I have not mentioned yet. This is a expression call that leverages a fluent-interface object that allows you to easily set route values, route value formats, and html attributes. Let’s take a look at the signature of the fluent-interface object:

    strong-typed-routes action-link-settings   

    As you can see you can  apply single or multiple attributes, formats or values. And for attributes I used the JQuery ‘Attr’ for html attributes. (Important note: any formats or route values specified here will override those specified by by the RouteValueAttribute applied to the action method or the method expression provided by calling the Html.ActionLink<C> extension or Url.Action<C> extensions. Phew!)

    In this case we are submitting a route value for ‘_RouteName’ of ‘TypedRoutes1’.

Here is the resulting route:

/TypedRoutes1/Sample1 

Here is a brief run down:

  • The Date RouteValueAttribute default is matched so the value from the ActionLink extension is not passed along in the route.
  • The route ‘TypedRoutes1’ is called specified by supplying the appropriate ‘_RouteName’ value.

Sample ActionLink Route 2

 strong-typed-routes sample-2

Let’s look at the differences:

  • We are calling the same action method as in sample 1 above, but this time we are specifying the current time.
  • This time we are submitting a route value for ‘_RouteName’ of ‘TypedRoutes2’.

Here is the resulting route:

/TypedRoutes2/Sample1?Date=2009-10-18

Notice it correctly chose the ‘TypedRoutes2’ route and submitted a date value formatted as specified by the RouteValueAttribute applied to the method Date parameter.

Sample ActionLink Route 3

strong-typed-routes sample-3

Here is the brief run down:

  • This sample calls a different action, one that takes a complex object.
  • Like sample 2above we are submitting a route value for ‘_RouteName’ of ‘TypedRoutes2’.
  • Also, if we look at the controller it has a BindAttribute applied with a prefix. This will be used in formatting values in the route.
  • Also we are specifying a custom format for ‘TestObject.Date’.

Here is the resulting route:

/TypedRoutes2/Sample2?TestObject.Name=Sample 2 Link&TestObject.Date=2009-10-18

 

That’s It for Now

But before I go a few additional notes:

  • This was compiled for MVC 2 Preview 2 but can easily be made to work under MVC 1 by removing the ‘ToHtmlString’ method calls which are new in MVC 2 Preview 2.
  • The RouteValueAtrribute can be applied to properties in complex objects being used as action method parameters, but I have not made extensive testing of this.
  • There are additional views in the sample project that shows off the subtle power of using the ‘PassThru’ property of the RouteActionAttribute.
  • There are also Action methods of the Url helper for use in view or controllers.

Written by Lynn Eriksen

March 28, 2010 at 11:53 am

Posted in Uncategorized

Tagged with