Saturday, May 17, 2008

As we all know, ASP.NET uses one form model to do all it's magic. Love it or hate it, we have to live with it. 99% of the time, it is not a problem, but then there is this 1%...

The main problem with ASP.NET's model is that the form tag has a fixed action attribute. What if we want to change it? No, there is no simple way of doing it. Solutions vary. Most common one is to create your own FormControl that inherits from HtmlForm and override RenderAttributes method. Unfortunately, inside that method, we have to do everything the original method does and only change the action attribute to the desired value. Ugly!

Another one is to provide an OutputFilter for the Response but that is also a lot of coding.

My preferred way of dealing with this issue is to provide my own, special HtmlTextWriter. Its role is to intercept the WriteAttribute method call and provide my own value. Following code illustrates the concept:

class ActionAttributeWriter
    : HtmlTextWriter
{
    string action;
    public ActionAttributeWriter(
        HtmlTextWriter writer, string action)
        : base(writer)
    {
        this.action = action;
    }
    public override void WriteAttribute
        (string name, string value, bool fEncode)
    {
        if (name == "action")
        {
            value = action; 
        }
        base.WriteAttribute(name, value, fEncode);
    }
}

Only thing that is needed then is to use this writer instead of a normal one. Page.CreateHtmlTextWriter can be used for this purpose:

protected override HtmlTextWriter 
    CreateHtmlTextWriter(System.IO.TextWriter tw)
{
    ActionAttributeWriter writer = 
        new ActionAttributeWriter(
            base.CreateHtmlTextWriter(tw), 
            "action");
    return writer;
}

Solution is not very clean and I would certainly not recommend it for a public product, but it's better than nothing.

Sunday, May 18, 2008 9:54:32 AM (Central European Standard Time, UTC+01:00)
Hi

have you seen Scott's article about url rewritting? he addresses this problem.

http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
Sean Rock
Saturday, December 05, 2009 5:11:39 PM (Central European Standard Time, UTC+01:00)
I really enjoyed it.
I am from Singapore and learning to read in English, give true I wrote the following sentence: "During his spy, lorre was a body of sigmund freud, casino royale."

With love ;), Kirby.
Tuesday, July 13, 2010 3:38:27 AM (Central European Standard Time, UTC+01:00)
Hello everybody, today really lucky
To introduce you these good online shops now!

Sale: GUCCI Bags, GUCCI Handbags, GUCCI Shoes, GUCCI Clothes, GUCCI Glasses, etc.
http://www.gucciboss.com/

2010 NFL, NFL Jerseys, NFL Jersey, NHL Jerseys, MLB Jerseys, NBA Jerseys, wholesale NFL Jerseys
http://www.2010-nfl.com/


Vibram,Five Fingers,fivefingers,Five Fingers Kso,Five Fingers Classic,Five Fingers Sprint,FiveFingers shoes,Vibram Outlet Sale
http://www.topfivefingers.com/

Christian Louboutin,Christian Louboutin shoes,Christian Louboutin sale,ChristianLouboutin Boots,Christian Louboutin Pumps,Christian Louboutin Sandals
http://www.superchristianlouboutin.com
Thursday, August 26, 2010 8:34:05 AM (Central European Standard Time, UTC+01:00)
http://www.gucciall.com
Discount Gucci, Discount Gucci Handbags, Gucci Sunglasses

http://www.wholesale-ugg-boots.com
UGG, UGG Boots, cheap UGG Boots

http://fivefingersshoes.tk
http://mychristianlouboutin.tk
http://ghdstraighteners.tk
http://kidsjerseys.tk
http://ghdshopping.tk
http://guccigucci.tk
http://saleshoes.tk
http://thehandbags.tk


http://ilikehandbags.tk
http://ilikejerseys.tk
http://isshoes.tk
http://rarehandbags.tk
http://rarejerseys.tk
http://rareshoes.tk
http://sellhandbags.tk
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview