Tag Archives: MVC

Make IE displays JSON file to the screen

When we have a JSON builder, say a MVC Controller with method returns JSON, and we want to debug this method using Internet Explorer, IE will prompt for download the JSON file hence we can’t see the result immediately. Really annoying behavior.

However we can alter this by update Windows Registry. Below are the steps on how to do so:

  1. We need to be login as user with administrator privilege
  2. Open any text editor (Notepad will do) and create a file called js_ie.reg or any name with .reg extension and put below as content

    Windows Registry Editor Version 5.00
    ;
    ; Tell IE to open JSON documents in the browser. 
    ; 25336920-03F9-11cf-8FD0-00AA00686F13 is the CLSID for the "Browse in place" .

    [HKEY_CLASSES_ROOT\MIME\Database\Content Type\application/json]
    "CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
    "Encoding"=hex:08,00,00,00

    [HKEY_CLASSES_ROOT\MIME\Database\Content Type\text/json]
    "CLSID"="{25336920-03F9-11cf-8FD0-00AA00686F13}"
    "Encoding"=hex:08,00,00,00

  3. Double click .reg file, it will prompt us for confirmation to change registry, choose OK
  4. Now, you our IE will immediately shows JSON results on screen rather than prompt us to download

Return simple string on ASP.NET MVC

Today I teach some of my junior programmer on how to work with ASP.NET MVC, first question they ask once they see the sample of MVC from standard Visual Studio 2010 template is “what if I want only return Hello World as the output?”, coincidently this question is the same question always being asked to me by MVC newbie.

MVC concept segregates layer of development into Model, View and Controller. Most of time developer effort will be on Controller and View and these 2 layers have a mutual relationship, because Controller will handle all business logic and/or data processing while View is meant for UI handling purpose. Sometime as programmer, we will need to test our business logic function has proper output or not, and the output normally just simple object or value. Build a different view to render the output seems too many effort to do and will add more “to-be-deleted” file on our solution, hence question on how to send simple output directly from Controller is really logical.

Okay, back to the issue, what is the solution? ASP.NET MVC have a class “ContentResult” to accomodate this problem. Code should be like below:

 

   1:  public class HomeController : Controller
   2:      {
   3:          public ActionResult Index()
   4:          {
   5:              String ret = "Hello World!";
   6:   
   7:              //-- build return string here
   8:              return Content(ret);
   9:          }
  10:     }

Have a nice MVC Controller test !!!