Reputation: 3
Why does an ASP.NET button (by default) make POST request? Is it possible to alter this behavior? I need to have my button make a GET request.
I tried to set the HTTP data transfer method to GET by doing this --
Dim context As HttpContext = System.Web.HttpContext.Current
Dim request As HttpRequest = context.Request
request.RequestType = "GET"
But this doesn't work.
Upvotes: 0
Views: 1042
Reputation: 15860
This behaviour is caused by a form with method="post"
. You can change this, by using
<form>
<input type="text" name="name" />
<input type="button" value="Save" />
</form>
This will make a GET
request. Default is GET
. I don't know why your request was being a POST
but the only behaviour that might cause this would be the fact that the form your button is residing in is having method="post"
. This way, the request being made is now a POST one.
Change that thing, and you will get the output that you're wanting.
Upvotes: 1
Reputation: 39777
You can specify method in the form, e.g.
<form id="form1" runat="server" method="get">
<asp:Button ID="Button1" runat="server" Text="Button" />
</form>
But this will pass everything in querystring - including ViewState, you will see things like
...Default.aspx?__VIEWSTATE=%2FwEPDwUKMjA0OTM4MTAwNGRkWg%2FUFN%2FiSKnn0PXnaxicZZP%2BkRo%3D&__EVENTVALIDATION=%2FwEWAgKZlprMBQKM54rGBlcwa3uNV3%2BKkm7r8IYXTcNXsc1W&Button1=Button
So I do not think you want this. Also querystring length is very limited comparing to post body. If you want to use GET to pass something in a querystring - use HyperLink control and its NavigateURL property.
Upvotes: 1
Reputation: 27472
I'm not sure that it's possible to make a straightforward ASP.NET button send a GET request. ASP.NET stores a ton of information on the page and returns this on the post back -- the view state. This is probably too much for a get.
You can easily create a link that will do a GET request with an asp:hyperlink tag. You could create an HTML button object that has onclick behavior that does windows.location= to effectively act like a GET.
You cannot create another form on your page that does a GET, because an ASP.NET page can only have one form.
Upvotes: 1