Mvc icon indicating copy to clipboard operation
Mvc copied to clipboard

RC2 : Url generated by custom FormTagHelper is encoded

Open marcusien opened this issue 10 years ago • 4 comments

I created a custom FormTagHelper. In the Process method, I concatenate some parameters with Action

   public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            this.Action += "?firstParameter=1&secondParameter=2"; // <=== merging parameters with url of form action attribute
            base.Process(context, output);
        }

The issue is that the generated url inside HTML page is the following

/Home/IndexPost%3FfirstParameter%3D1%26secondParameter%3D2

Expected url is

/Home/IndexPost?firstParameter=1&secondParameter=2

There was no problem with RC1 WebApplication1.zip

marcusien avatar Jun 08 '16 14:06 marcusien

Found a workaround by changing my initial code by this one

public override void Process(TagHelperContext context, TagHelperOutput output)
{
            base.Process(context, output);
            var actionAttribute = output.Attributes["action"];
            var actionWithUrlParameters = actionAttribute.Value.ToString() + urlParameters;
            output.Attributes.Remove(actionAttribute);
            output.Attributes.Add("action", actionWithUrlParameters);
}

But it looks dirty ! Why encode the Action ?

marcusien avatar Jun 08 '16 15:06 marcusien

There was no problem with RC1

Sounds like an RC1 bug. The Action property of the FormTagHelper is an input to the tag helper and not the generated action attribute. It's actually associated with the asp-action attribute in the .cshtml file. That gets URL-encoded because it should be a valid segment of the URL's path.

WebApplication1.zip

Please do not attach Zip files in issues. We're not likely to download it and git makes it much easier to provide feedback on your specific project. Instead use a GitHub repo.

But it looks dirty !

Could use output.Attributes.SetAttribute() instead of the Remove() and Add().

Why encode the Action?

Your updated code is still HTML-encoding the value (view the page source to confirm). URL-encoding was happening due to earlier misuse of the Action property.

dougbu avatar Jun 08 '16 15:06 dougbu

Sounds like an RC1 bug. The Action property of the FormTagHelper is an input to the tag helper and not the generated action attribute. It's actually associated with the asp-action attribute in the .cshtml file. That gets URL-encoded because it should be a valid segment of the URL's path.

I think I miss a step. The provided url is not the value of Action inside the code but after HTML rendering. In the Process method, the Action value is originally just "IndexPost" and I concatenate with my parameters. Before the method calls Process(...), the Action value is "IndexPost?firstParameter=1&secondParameter=2", and after call it's "IndexPost%3FfirstParameter%3D1%26secondParameter%3D2"

I think the reason for encoding is if there's special characters on the Action name but for me it's not mandatory cause you're not supposed to have special characters on a method (action) name

Sorry for the zip file.

Could use output.Attributes.SetAttribute() instead of the Remove() and Add().

I didn't saw this method :) thank you

Your updated code is still HTML-encoding the value (view the page source to confirm). URL-encoding was happening due to earlier misuse of the Action property.

I don't understand what you mean. my 'workaround' works fine, the generated url is now good => http://localhost:port/Home/IndexPost?firstParameter=1&secondParameter=2

Maybe this code is the right thing to do I don't know but it's weird for me to encode the Action in the process stuff

marcusien avatar Jun 09 '16 06:06 marcusien

The workaround listed in https://github.com/aspnet/Mvc/issues/4833#issuecomment-224623855 is the correct way to do this.

Eilon avatar Jul 05 '17 21:07 Eilon