ASP.NET MVC, Webform hybrid
Posted
by Greg Ogle
on Stack Overflow
See other posts from Stack Overflow
or by Greg Ogle
Published on 2009-08-14T14:40:52Z
Indexed on
2010/04/25
12:13 UTC
Read the original article
Hit count: 486
We (me and my team) have a ASP.NET MVC application and we are integrating a page or two that are Web Forms. We are trying to reuse the Master Page from our MVC part of the app in the WebForms part. We have found a way of rendering an MVC partial view in web forms, which works great, until we try and do a postback, which is the reason for using a WebForm.
The Error:
Validation of viewstate MAC failed. If this application is hosted by a Web Farm or cluster, ensure that configuration specifies the same validationKey and validation algorithm. AutoGenerate cannot be used in a cluster.
The Code to render the partial view from a WebForm (credited to "How to include a partial view inside a webform"):
public static class WebFormMVCUtil
{
public static void RenderPartial(string partialName, object model)
{
//get a wrapper for the legacy WebForm context
var httpCtx = new HttpContextWrapper(System.Web.HttpContext.Current);
//create a mock route that points to the empty controller
var rt = new RouteData();
rt.Values.Add("controller", "WebFormController");
//create a controller context for the route and http context
var ctx = new ControllerContext(
new RequestContext(httpCtx, rt), new WebFormController());
//find the partial view using the viewengine
var view = ViewEngines.Engines.FindPartialView(ctx, partialName).View;
//create a view context and assign the model
var vctx = new ViewContext(ctx, view,
new ViewDataDictionary { Model = model },
new TempDataDictionary());
//ERROR OCCURS ON THIS LINE
view.Render(vctx, System.Web.HttpContext.Current.Response.Output);
}
}
My only experience with this error is in context of a web farm, which is not the case. Also, I understand that the machine key is used for decrypting the ViewState.
Any information on how to diagnose this issue would be appreciated.
A Work-around: So far the work-around is to move the header content to a PartialView, then use an AJAX call to call a page with just the Partial View from the WebForms, and then using the PartialView directly on the MVC Views. Also, we are still able to share non-tech-specific parts of the Master Page, i.e. anything that is not MVC specific. Still yet, this is not an ideal solution, a server-side solution is still desired.
Also, this solutino has issues when working with controls that have more sophisticated controls, using JavaScript, particularly dynamically generated script as used by 3rd party controls.
© Stack Overflow or respective owner