The attribute 'action' may not appear in tag 'FORM [method=POST]' while making AMP pages in asp.net

2.3k Views Asked by At

I have a website in asp.net and I am working on creating AMP pages at http://mywebsite.com/amp/......

I was able to make all the necessary changes for AMP except the following error.

The attribute 'action' may not appear in tag 'FORM [method=POST]'

the Form tag in my website looks like this

<form name="form1" method="post" id="form1">

AMP tutorial suggest to use action-xhr=/default.aspx instead of action="default.aspx

here is the source: https://ampbyexample.com/components/amp-form/

Now, the thing is how to remove action attribute and add action-xhr attribute in asp.net.

I tried following code in cs file but no luck.

this.Page.Form.Attributes.Remove("action");

Please note JavaScript and jQuery are not allowed in AMP

2

There are 2 best solutions below

0
On

Add this script in the head tag (this will make your form to amp-form):

<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>

Now, your form can have action-xhr attribute.

<form name="form1" method="post" id="form1" action-xhr="__url__">

and

0
On

Take a look at the MSDN documentation, where it talks about an ActionlessForm. You basically inherit HtmlForm, and replace the form tag on the aspx page with your new control. You'll have to replace a bit of code like they suggest with the following:

base.Attributes.Remove("action");
writer.WriteAttribute("action-xhr", this.Action);

Worked like a charm for me. You may need to replace this.Action with this.Page.Request.Path, and you may find that you need to also add a target attribute, but this will definitely put you on the right path.