asp.net mvc 3 - Changing the MasterName during ActionFilter.OnResultExecuting throws exception -


i have action filter changes master/layout page @ runtime based on whether ajax call. fires system.web.httpexception when ajax call. actionfilter works fine webforms engine throw exception razorengine. exception:

the "renderbody" method has not been called layout page "~/views/xxxx/empty.cshtml"

the action filter:

public class ajaxactionfilterattribute : actionfilterattribute {     public string masterpage { get; set; }      public override void onresultexecuting(resultexecutingcontext filtercontext)     {         if (filtercontext.httpcontext.request.headers["x-requested-with"] != null             && filtercontext.httpcontext.request.headers["x-requested-with"] == "xmlhttprequest")         {             var viewresult = filtercontext.result viewresult;             if (viewresult != null)             {                 viewresult.mastername = masterpage;             }         }     } } 

my action marked attribute follows:

[ajaxactionfilter(masterpage = "empty")] [acceptverbs(httpverbs.get), actionname("index"), collectionaction] public override actionresult collectionaction() {     ... } 

edit: , of course layout view being empty.cshtml follows:

    <html xmlns="http://www.w3.org/1999/xhtml">     <head id="defaultmasterhead">        @rendersection("styles", false)     </head>    <body>     <div>         @renderbody()     </div>         @rendersection("scripts", false)     </body>     </html> 

if hit view non-ajax call fine if ajax call throws exception. again, works correctly webforms engine.

can explain might going on here?

tia

try overriding onactionexecuted instead of onresultexecuting:

public class ajaxactionfilterattribute : actionfilterattribute {     public string masterpage { get; set; }      public override void onactionexecuted(actionexecutedcontext filtercontext)     {         base.onactionexecuted(filtercontext);         var result = filtercontext.result viewresult;         if (filtercontext.httpcontext.request.isajaxrequest() && result != null)         {             result.mastername = masterpage;         }     } } 

Comments