Post Data to ASP.NET Page From PHP or Javascript



Background:
For most PHP developers, the technique of posting data from one page to a different page is not a difficult thing. In fact, in PHP development, this technique is learnt by beginners. In ASP.Net however, it doesn't work the same way. The ASP.Net platform is designed to implicitly post data to itself and developers need not manually trap posted data themselves.

Issue:
This simplistic development approach implemented by ASP.Net poses a problem where one needs to for one reason or the other, post to the ASP.Net page from another ASP.Net page, a PHP page, from JavaScript or even Python, ruby, and any other server side platform you can imagine. In my own case, I was using dropzone.js to implement drag-and-drop file upload for an application. I would have to post to a server side platform using JavaScript. For some reasons, I didn't want to use PHP, even though PHP would work seamlessly. The platform available for me was ASP.Net, but remember; ASP.Net doesn't allow unsolicited posting of data to its pages! or so I thought.

Solution:
ASP.Net actually allows posting of data like PHP does, and one could actually develop with ASP.Net as if it were PHP using <form action="somepage.aspx" runat="server" method="POST">. I have the VisualStudio environment too thank for this breakthrough. After searching the web for a full day without much progress, I resorted to trial and error. The VisualStudio IDE gives lots of explanations for programming elements through its powerful intellisense. The Page.Request property exposes lots of useful members with which you may interact with the current request to your page.

Instances:
In order to access request headers use: Request.Headers["header_name"]
In order to access files posted use: Request.Files
In order to access form data use: Request.Form.Get("data_name")
You may also return data to the calling platform using Response.Write("some data")

What a breakthrough!

Comments

Popular posts from this blog

Resize or Crop Image before Upload Using HTML5 File API

Get Creative With Data Tables: Row Click Events

Exception from HRESULT: 0x80131040