我正在使用ASP.NET 3.5 SP1 System.Web.Routing与经典WebForms,如http://chriscavanagh.wordpress.com/2008/04/25/systemwebrouting-with-webforms-sample/中所述
一切正常,我有自定义SEO网址甚至回发作品.但有一种情况是回发总是失败,我得到一个:
验证视图状态MAC失败.如果此应用程序由Web场或群集托管,请确保配置指定相同的validationKey和验证算法.AutoGenerate不能在群集中使用.
以下是重现错误的方案:
使用按钮创建标准webform mypage.aspx
创建一个将"a/b/{id}"映射到"〜/ mypage.aspx"的路线
当您执行该站点时,您可以导航http:// localhost:XXXX/a/b /页面工作的内容.但是当你按下按钮时会出现错误.当Route只是"a/{id}"时,不会发生错误.
它似乎与url中的子路径数有关.如果至少有2个子路径,则视图状态验证失败.
即使使用EnableViewStateMac ="false",您也会收到错误.
有任何想法吗?这是一个错误吗?
谢谢
我通过让我的视图用户控件从这个类继承而不是ViewUserControl
(它是RenderView的补丁)来解决这个问题.它为我做了伎俩,希望它也适合你.
public class ViewUserControlWithoutViewState: ViewUserControl where T : class { protected override void LoadViewState(object savedState) {} protected override object SaveControlState() { return null; } protected override void LoadControlState(object savedState) {} protected override object SaveViewState() { return null; } /// /// extracted from System.Web.Mvc.ViewUserControl /// /// public override void RenderView(ViewContext viewContext) { viewContext.HttpContext.Response.Cache.SetExpires(DateTime.Now); var containerPage = new ViewUserControlContainerPage(this); ID = Guid.NewGuid().ToString(); RenderViewAndRestoreContentType(containerPage, viewContext); } ////// extracted from System.Web.Mvc.ViewUserControl /// /// /// public static void RenderViewAndRestoreContentType(ViewPage containerPage, ViewContext viewContext) { string contentType = viewContext.HttpContext.Response.ContentType; containerPage.RenderView(viewContext); viewContext.HttpContext.Response.ContentType = contentType; } ////// Extracted from System.Web.Mvc.ViewUserControl+ViewUserControlContainerPage /// private sealed class ViewUserControlContainerPage : ViewPage { // Methods public ViewUserControlContainerPage(ViewUserControl userControl) { Controls.Add(userControl); EnableViewState = false; } protected override object LoadPageStateFromPersistenceMedium() { return null; } protected override void SavePageStateToPersistenceMedium(object state) {} } }
我不久前在博客上发表过这篇文章.