Why is always MasterName blank in OnActionExecuted?
I'm trying to get the master page changed for all my aspx pages. For some reason I'm unable to detect when this function is called for a ascx page instead. Any help in correting this would be appreciated.
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
var action 开发者_JAVA百科= filterContext.Result as ViewResult;
if (action != null && action.MasterName != "" && Request.IsAjaxRequest())
{
action.MasterName = "Ajax";
}
base.OnActionExecuted(filterContext);
}
If you're still keen on changing the master page based on the fact whether your request is ajax or not - I just accidentally stumbled on exactly the thing you were looking for:
http://devlicio.us/blogs/sergio_pereira/archive/2008/12/05/reusing-views-in-asp-net-mvc-for-ajax-updates.aspx
Basically, instead of overriging the OnActionExecuting method in the BaseController - override the View method! You get exactly the thing you want, with a method that seems specifically designed for it :)
protected override ViewResult View(string viewName, string masterName, object model)
{
return base.View(viewName, Request.IsAjaxRequest() ? "Empty" : masterName, model);
}
So you're saying that MasterPage is empty when you're executing actions to .ascx "pages"?
.ascx's aren't pages, they're UserControls / PartialViews. And as such they don't have master pages. They can be dropped in a mage, or a master page.. But if your request is returning an .ascx, it won't have a master page.. )
UPD: This is most likely because of the way MVC works - all the 3 parts (M-V-C) are completely independant. Which means that when your code executes inside the controller, we know nothing at all about the view. And the View is the one that selects the master page, not the controller.
Tbh if you're trying to change the way the app looks (change the master page) inside the controller - you're most likely doing something wrong. It was designed with separation of context in the first place, and you're trying to go around it :)
UPD2: So you're saying that you want to return the full page + master page for regular requests, and just the page without the master (well, clean at least) for ajax requests? You're still trying the wrong approach.
Here's what I've been doing instead:
if (!Request.IsAjaxRequest())
return View(model);
else
return PartialView("PartialName", model);
Exactly the same situation. If I'm loading the url in a browser - it returns the full page, master and all.. If I'm loading it later on, using an ajax call - just load the partial view. Simple and easy. And still adheres to the MVC methodology :)
Also, if you're absolutely keen on preselecting the master name.. just do it like this:
return View("ViewName", "MasterName", model);
精彩评论