Shaun Xu

The Sheep-Pen of the Shaun


News

logo

Shaun, the author of this blog is a semi-geek, clumsy developer, passionate speaker and incapable architect with about 10 years’ experience in .NET and JavaScript. He hopes to prove that software development is art rather than manufacturing. He's into cloud computing platform and technologies (Windows Azure, Amazon and Aliyun) and right now, Shaun is being attracted by JavaScript (Angular.js and Node.js) and he likes it.

Shaun is working at Worktile Inc. as the chief architect for overall design and develop worktile, a web-based collaboration and task management tool, and lesschat, a real-time communication aggregation tool.

MVP

My Stats

  • Posts - 122
  • Comments - 542
  • Trackbacks - 0

Tag Cloud


Recent Comments


Recent Posts


Article Categories


Archives


Post Categories


.NET



RenderAction method had been introduced when ASP.NET MVC 1.0 released in its MvcFuture assembly and then final announced along with the ASP.NET MVC 2.0. Similar as RenderPartial, the RenderAction can display some HTML markups which defined in a partial view in any parent views. But the RenderAction gives us the ability to populate the data from an action which may different from the action which populating the main view.

For example, in Home/Index.aspx we can invoke the Html.RenderPartial(“MyPartialView”) but the data of MyPartialView must be populated by the Index action of the Home controller. If we need the MyPartialView to be shown in Product/Create.aspx we have to copy (or invoke) the relevant code from the Index action in Home controller to the Create action in the Product controller which is painful. But if we are using Html.RenderAction we can tell the ASP.NET MVC from which action/controller the data should be populated. in that way in the Home/Index.aspx and Product/Create.aspx views we just need to call Html.RenderAction(“CreateMyPartialView”, “MyPartialView”) so it will invoke the CreateMyPartialView action in MyPartialView controller regardless from which main view.

But in my current project we found a bug when I implement a RenderAction method in the master page to show something that need to connect to the backend data center when the validation logic was failed on some pages. I created a sample application below.

 

Demo application

I created an ASP.NET MVC 2 application and here I need to display the current date and time on the master page. I created an action in the Home controller named TimeSlot and stored the current date into ViewDate. This method was marked as HttpGet as it just retrieves some data instead of changing anything.

   1: [HttpGet]
   2: public ActionResult TimeSlot()
   3: {
   4:     ViewData["timeslot"] = DateTime.Now;
   5:     return View("TimeSlot");
   6: }

Next, I created a partial view under the Shared folder to display the date and time string.

   1: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
   2:  
   3: <span>Now: <%
   1: : ViewData["timeslot"].ToString() 
%></span>

Then at the master page I used Html.RenderAction to display it in front of the logon link.

   1: <div id="logindisplay">
   2:     <%
   1:  Html.RenderAction("TimeSlot", "Home"); 
%>
   3:  
   4:     <%
   1:  Html.RenderPartial("LogOnUserControl"); 
%>
   5: </div> 

It’s fairly simple and works well when I navigated to any pages. But when I moved to the logon page and click the LogOn button without input anything in username and password the validation failed and my website crashed with the beautiful yellow page. (I really like its color style and fonts…)

image

 

How ASP.NET MVC executes Html.RenderAction

In this example all other pages were rendered successful which means the ASP.NET MVC found the TimeSolt action under the Home controller except this situation. The only different is that when I clicked the LogOn button the browser send an HttpPost request to the server. Is that the reason of this bug? I created another action in Home controller with the same action name but for HttpPost.

   1: [HttpPost]
   2: [ActionName("TimeSlot")]
   3: public ActionResult TimeSlot(object dummy)
   4: {
   5:     return TimeSlot();
   6: }

Or, I can use the AcceptVerbsAttribute on the TimeSlot action to let it allow both HttpGet and HttpPost.

   1: [AcceptVerbs("GET", "POST")]
   2: public ActionResult TimeSlot()
   3: {
   4:     ViewData["timeslot"] = DateTime.Now;
   5:     return View("TimeSlot");
   6: }

And then repeat what I did before and this time it worked well.

image

Why we need the action for HttpPost here as it’s just data retrieving? That is because of how ASP.NET MVC executes the RenderAction method.

In the source code of ASP.NET MVC we can see when proforming the RenderAction ASP.NET MVC creates a RequestContext instance from the current RequestContext and created a ChildActionMvcHandler instance which inherits from MvcHandler class. Then the ASP.NET MVC processes the handler through the HttpContext.Server.Execute method. That means it performs the action as a stand-alone request asynchronously and flush the result into the  TextWriter which is being used to render the current page. Since when I clicked the LogOn the request was in HttpPost so when ASP.NET MVC processed the ChildActionMvcHandler it would find the action which allow the current request method, which is HttpPost. Then our TimeSlot method in HttpGet would not be matched.

image

 

Summary

In this post I introduced a bug in my currently developing project regards the new Html.RenderAction method provided within ASP.NET MVC 2 when processing a HttpPost request. In ASP.NET MVC world the underlying Http information became more important than in ASP.NET WebForm world. We need to pay more attention on which kind of request it currently created and how ASP.NET MVC processes.

 

Hope this helps,

Shaun

 

All documents and related graphics, codes are provided "AS IS" without warranty of any kind.
Copyright © Shaun Ziyan Xu. This work is licensed under the Creative Commons License.

Comments

Gravatar # re: Html.RenderAction Failed when Validation Failed
Posted by anytao on 5/19/2010 1:57 PM
Please don't use it in a techinical interview. :-)
Gravatar # Website Validation
Posted by w3cvalidation on 5/24/2010 7:59 PM
Nice information, I really appreciate the way you presented.Thanks for sharing..
Post A Comment
Title:
Name:
Email:
Comment:
Verification: