2009-08-14 85 views
3

我们(我和我的团队)有一个ASP.NET MVC应用程序,我们正在集成一个或两个Web窗体。我们试图在WebForms部分的应用程序的MVC部分中重用Master Page。我们发现在渲染网页表单的MVC局部视图,它的伟大工程,直到我们尝试做回发,这是使用一个WebForm的原因的一种方式。ASP.NET MVC,Webform混合

的错误:

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.

代码从一个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); 
    } 
} 

我这个错误唯一的经验是在Web场,这是不是上下文案子。另外,我知道机器密钥用于解密ViewState。

有关如何诊断此问题的任何信息,将不胜感激。

一个变通办法: 到目前为止的解决办法是在头内容移动到PartialView,然后使用AJAX调用来调用页面只偏查看从Web窗体,然后使用直接在MVC视图上进行部分视图。此外,我们仍然可以分享Master Page的非技术特定部分,即任何非MVC特定的部分。但是,这还不是一个理想的解决方案,仍然需要服务器端解决方案。

而且,与具有更复杂的控制的控制工作时,使用JavaScript,13759第三方控件尤其动态生成的脚本这个solutino有问题。

+1

我正在寻找解决方案,它似乎你也在SO问题ID#702746中使用相同的技术。任何人?? – 2009-08-14 15:07:49

+0

@Kurt编辑制作给予信贷 – 2009-08-14 15:12:00

+0

重复:http://stackoverflow.com/questions/230014 – 2009-08-15 13:33:15

回答

0

尝试在所有MVC页面禁用ViewState中明确。

1

Mauricio Scheffer's answer

有部分观点继承他的ViewUserControlWithoutViewState<T>类,而不是ViewUserControl<T>解决了这个对我来说。

0

如何禁用MAC验证?

这里是一个链接

http://msdn.microsoft.com/en-us/library/ydy4x04a.aspx

EnableViewStateMac =假

可以忽略

Security Note

This attribute should never be set to false in a production Web site.

,因为你的网站是一个MVC网站和视图状态不是真正使用。