views:

33

answers:

1

Hello everybody,

I have this in my 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");
    }
}

it works fine, but I sometimes get this 500 unable to validate data so i guess that is because the checksum is generated on behalf of the url. which does not match the viewstate.

So how do you solve it so that you can use RewritePath but don't get the 500 errors?

Edit forgot to mention that i have a static machinekey validationkey in the web.config

Edit2 found someone else has exactly the same problem: http://bytes.com/topic/asp-net/answers/298680-form-action-context-rewritepath#post1172026

the rewritepath causes an invalid viewstate when there is a postback

A: 

Switching from to maproutes from System.Web.Routing

Old code:

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");
    }
}

New code:

source: 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");
}
JP Hellemons