gpt4 book ai didi

ASP.NET MVC、Webform 混合

转载 作者:行者123 更新时间:2023-12-02 09:36:11 29 4
gpt4 key购买 nike

我们(我和我的团队)有一个 ASP.NET MVC 应用程序,并且我们正在集成一两个 Web 表单页面。我们正在尝试在 WebForms 部分中重用应用程序的 MVC 部分中的母版页。我们找到了一种在 Web 表单中呈现 MVC 部分 View 的方法,该方法效果很好,直到我们尝试进行回发,这就是使用 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 渲染部分 View 的代码 (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);
}
}

我遇到此错误的唯一经历是在网络场的上下文中,但事实并非如此。另外,我知道机器 key 用于解密 ViewState。

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

解决方法:到目前为止,解决方法是将 header 内容移动到 PartialView,然后使用 AJAX 调用从 WebForms 调用仅包含部分 View 的页面,然后直接在 MVC View 上使用 PartialView。此外,我们仍然能够共享母版页的非技术特定部分,即非 MVC 特定的任何内容。但这仍然不是一个理想的解决方案,仍然需要服务器端解决方案。

此外,该解决方案在使用 JavaScript(尤其是第 3 方控件使用的动态生成脚本)处理具有更复杂控件的控件时会出现问题。

最佳答案

参见Mauricio Scheffer's answer .

将部分 View 子类化为 ViewUserControlWithoutViewState<T>类而不是 ViewUserControl<T>为我解决了这个问题。

关于ASP.NET MVC、Webform 混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1278258/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com