5 viewstate asp.net-mvc routing mac-address
我正在使用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不能在群集中使用.
以下是重现错误的方案:
它似乎与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) {}
}
}
Run Code Online (Sandbox Code Playgroud)
我不久前在博客上发表过这篇文章.
| 归档时间: |
|
| 查看次数: |
8720 次 |
| 最近记录: |