我正在使用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/something页面工作.但是当你按下按钮时会出现错误.当Route只是“a / {id}”时,不会发生错误.
它似乎与url中的子路径数有关.如果至少有2个子路径,则视图状态验证失败.
即使使用EnableViewStateMac =“false”,您也会收到错误.
有任何想法吗?这是一个错误吗?
谢谢
解决方法
我通过让我的视图用户控件继承自此类而不是ViewUserControl< T>来解决这个问题. (这是RenderView的补丁).它为我做了伎俩,希望它也适合你.
public class ViewUserControlWithoutViewState<T> : ViewUserControl<T> 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; } /// <summary> /// extracted from System.Web.Mvc.ViewUserControl /// </summary> /// <param name="viewContext"></param> 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); } /// <summary> /// extracted from System.Web.Mvc.ViewUserControl /// </summary> /// <param name="containerPage"></param> /// <param name="viewContext"></param> public static void RenderViewAndRestoreContentType(ViewPage containerPage,ViewContext viewContext) { string contentType = viewContext.HttpContext.Response.ContentType; containerPage.RenderView(viewContext); viewContext.HttpContext.Response.ContentType = contentType; } /// <summary> /// Extracted from System.Web.Mvc.ViewUserControl+ViewUserControlContainerPage /// </summary> 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) {} } }
我blogged about this前一段时间.