Upgarde from Asp.Net MVC 1 to MVC 2 - how to and issues with JsonRequestBehavior

Posted by Renso on Geeks with Blogs See other posts from Geeks with Blogs or by Renso
Published on Thu, 30 Dec 2010 22:11:46 GMT Indexed on 2010/12/30 22:54 UTC
Read the original article Hit count: 325

Filed under:

Goal

Upgrade your MVC 1 app to MVC 2

Issues

You may get errors about your Json data being returned via a GET request violating security principles - we also address this here. This post is not intended to delve into why the Json GET request is or may be an issue, just how to resolve it as part of upgrading from MVC1 to 2.

Solution

First remove all references from your projects to the MVC 1 dll and replace it with the MVC 2 dll. Now update your web.config file in your web app root folder by simply changing references to assembly="System.Web.Mvc, Version 1.0.0.0 to Version 2.0.0.0, there are a couple of references in your config file, here are probably most of them you may have:

        <compilation debug="true" defaultLanguage="c#">
            <assemblies>
                       <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
            </assemblies>
        </compilation>
 

        <pages masterPageFile="~/Views/Masters/CRMTemplate.master" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validateRequest="False">
            <controls>
                <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
 

Secondly, if you return Json objects from an ajax call via the GET method you ahve several options to fix this depending on your situation:

1. The simplest, as in my case I did this for an internal web app, you may simply do:

            return Json(myObject, JsonRequestBehavior.AllowGet);
 

2. In Mvc if you have a controller base you could wrap the Json method with:

        public new JsonResult Json(object data)
        {
            return Json(data, "application/json", JsonRequestBehavior.AllowGet);           
        }
 

3. The most work would be to decorate your Actions with:

        [AcceptVerbs(HttpVerbs.Get)]
 

4. Another tnat is also a lot of work that needs to be done to every ajax call returning Json is:

                            msg = $.ajax({ url: $('#ajaxGetSampleUrl').val(), dataType: 'json', type: 'POST', async: false, data: { name: theClass }, success: function(data, result) { if (!result) alert('Failure to retrieve the Sample Data.'); } }).responseText;
 

This should cover all the issues you may run into when upgrading. Let me kow if you run into any other ones.

© Geeks with Blogs or respective owner