【问题标题】:Invalid viewstate when using RewritePath使用 RewritePath 时视图状态无效
【发布时间】:2011-04-20 18:41:43
【问题描述】:

我的 global.asax 中有这个

void Application_BeginRequest(object sender, EventArgs e) 
{
    string pathAndQuery = Request.Url.PathAndQuery.ToString().ToLower();
    if (pathAndQuery.Contains("prettyUrl"))
    {
        HttpContext.Current.RewritePath("Category.aspx?catg=uglyUrl");
    }
}

它工作正常,但我有时会得到这个 500 无法验证数据 所以我想这是因为校验和是代表 url 生成的。与视图状态不匹配。

那么你如何解决它,以便你可以使用RewritePath 但不会收到 500 错误?

编辑忘了提到我在 web.config 中有一个静态机器密钥验证密钥

Edit2发现其他人也有同样的问题:http://bytes.com/topic/asp-net/answers/298680-form-action-context-rewritepath#post1172026

当有回发时,重写路径会导致无效的视图状态

【问题讨论】:

  • 您有什么想问的吗?

标签: asp.net c#-4.0 url-rewriting viewstate


【解决方案1】:

System.Web.Routing切换到maproutes

旧代码:

void Application_BeginRequest(object sender, EventArgs e) 
{
    string pathAndQuery = Request.Url.PathAndQuery.ToString().ToLower();

    if (pathAndQuery.Contains("thisisawesome"))
    {
        HttpContext.Current.RewritePath("Products.aspx?catg=14&cat=161");
    }
}

新代码:

来源: http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes);
}

void RegisterRoutes(RouteCollection routes)
{
    routes.MapPageRoute("test", 
                        "thisisawesome", 
                        "~/Products.aspx?catg=14&cat=161");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    相关资源
    最近更新 更多