Tuesday, October 7, 2008

SEO friendly forms in ASP.NET

If you ever need to create a page in ASP.NET that uses some kind of Url mapper, you may run into some problems with posting back. If you look closer, you’ll find that the Url of the request is no longer the SEO friendly one but the actual one (possibly including some querystring parameters if they’re part of the mapping.) And .NET does not make it easy to override the address in the “action” attribute of your form.

The best way I’ve found to fix this problem is to create a Server control that inherits from the standard HtmlForm and overrides the RenderBeginTag() method.



You can now register and plug this new form into any page. You can then set the attribute AllowPostBack to false if there are no postbacks on your page. Or you can override PostBackUrl to set the “action” attribute on the form.

No comments:

Post a Comment