jQuery for ASP.NET MVC preview 2

by Chris van de Steeg. 10 Comments

I updated the jQueryMvc project so that it works with ASP.NET MVC preview 2. I also switched the IoC container from Objectbuilder to Unity. I submitted the Unity container to the MvcContrib project but it has not yet been accepted in their repository, so you’ll have to do with the unofficial version I include in the download. Unity is, to put it very baldly, Objectbuilder version 2. So it seemed logical to make the switch.

I also made up a nice(r) way to inject the ViewEngine into the BaseController. It’s now injected through a public method using a MethodInjection attribute:

image

Thanks to this method, we no longer have pre-register all the Controllers with our container (in the previous version of jQueryMvc I scanned all dll’s on ApplicationStart to register the Controller and assign the ServiceViewFactory as the default ViewFactory)

Furthermore, since the ASP.NET MVC team decided to change the way the Views are rendered (now through a ViewEngine, which is nice I think), it was no longer needed to use the code from Aaron Lerch. Though still inspired by his code ofcourse, I removed almost all of his code now. We now have a single ‘ServiceViewEngine‘ inheriting from the ASP.NET MVC’s default WebFormViewEngine. All code deciding what view to render, is now located in this class.

What I did not like about preview 2, is that the name of the master-template cannot be set from within the ViewEngine. It’s now stored into the ViewContext, and the setter is private :(. So now the mastertemplate is set in the BaseController, causing the BaseController to have to know what type of request the current one is (which is bad):

image

As usual, you can download the hottest source from the Subversion repository at http://code.google.com/p/jquerymvc/source/checkout or you can download a zipped package of the source code.

10 Responses to jQuery for ASP.NET MVC preview 2

  1. Jake Scott says:

    Hi Chris, this project rocks!

    I’ve run into a problem when the server needs to perform a response redirect.

    http://www.nikhilk.net/Ajax-MVC.aspx (has an explanation of the problem)

    The redirect is being handled internally by xmlhttprequest, and the ajax script on the page is getting the content of the redirected to page.

    You will need to send back a custom 302 response (i.e, not an actual 302, but a regular 200 response, whose response includes the fact that you need to redirect, and the redirect location).

    We need to handle the completed event of the ajax form to see if its this custom response, and instead of letting the update happen, call window.navigate with the new location.

    What do you think? Any cool ways to solve this will be much appreciated!

  2. Chris van de Steeg says:

    @Jake Scott: I think this can be solved using TempData on the controller. The fact that the request is json or ajax should be stored in the tempdata, and this should be considered by the ServiceViewEngine. I’ll look into this soon, but if you find a solution sooner, please let me know

  3. Jake Scott says:

    Hi Chris, is there anywhere I can post this conversation so that people can have a discussion and so that we dont spam your blog?

    http://blog.pengoworks.com/index.cfm/2007/10/9/Expiring-Session-via-AJAX-using-HTTP-Response-Headers
    has an example where he uses custom http response headers which indicate a session timeout. We could make this more generic and call it redirectTo? In the callback you can use getResponseHeader to see if this custom header exists and if it does cause the client to change its location.

    Also would this need to use the history plugin to change location?

    Obviously this is only required if the request is actually from ajax and if so we could store the redirectTo value in TempData and let the ServiceViewEngine add the custom http response header.

    Well thats a quick and nasty way :) what is everyones thoughts?

  4. mateo says:

    Hi Chris,

    great work this library is very of use.
    I’m having a problem with ajax query when using the Ajax.ActionLink “XMLHttpRequest”.Equals(request.Headers["X-Requested-With"]
    is never true cause i dont have the X-Requested-With passed in the header?

    Any idea why ?
    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>