3

I have inherited a web app and I need to convert a client side ajax post into server side asp.net code ( C#). I am not sure what the best approach is to accomplish this, I am pretty new to ajax posts but this code seems to be posting info to a page in the same project so I am assuming there is a much easier way to accomplish this server side just wanted to have someone confirm that I am not crazy...

Code

 $.ajax({
      //  type: "POST",
      //  url: '<%= ResolveUrl("~/default.aspx") %>/Login',
      //      data: parameters,
      //      contentType: "application/json; charset=utf-8",
      //      dataType: "json",
      //      success: function (msg) {

      //          if (msg.d == "success") {
      //              $.modal.autoResize = false;
      //              ResizeModal();
      //              var redirectUrl = $('#<%= btnSubmit.ClientID %>').attr('data-redirecturl');









      //              if (redirectUrl != null && redirectUrl.length > 0) {

      //                  window.location = redirectUrl;

      //              }
Sam Cromer
  • 687
  • 3
  • 12
  • 31
  • I guess it would be interesting when this ajax call is made - on button click perhaps? The login form should be in this case easily transferable to standard postback approach. – Adam Moszczyński Sep 19 '13 at 18:57
  • It is a little bit confusing "convert client side ajax post to server side C# asp.net code". What do you mean by that??? Ajax post it is an action initialized and triggered by client... – Alex Art. Sep 19 '13 at 18:58
  • Other that what the answers are suggesting, creating an HTTP request, you can potentially substitute the AJAX call with a simple function call server-side. Presumably you're just processing some data that has already reached the server, so there is no need to simulate a POST to your own application to do the processing - you can just call the processing function that a POST would call. – Alex Sep 19 '13 at 19:13
  • Yeah I know it's confusing while I was posting it I realized what the ajax code was doing, it will be much easier to move this into a server side function on the login page. I just wasn't sure why this was built this way. – Sam Cromer Sep 19 '13 at 19:35

3 Answers3

7

Ajax call is just a special case of HTTP request, there is no specific way out of the box in .net, so your question is about how to make HTTP request in .net and there are multiple ways:

Making and receiving an HTTP request in C# or using WebApi, which is the easiest way IMO.

and I would recommend to use Chrome Dev tools to capture exact HTTP request and then Fiddler to do the same for the server side and compare them to make sure they are similar.

BUT it looks like you need something slightly different, looks like your page just posting data to default page and then redirects to that page, that could be done by a simple form submit

Community
  • 1
  • 1
Restuta
  • 5,855
  • 33
  • 44
6

You can use an HttpWebRequest. It'll be something like this:

var httpWebRequest = (HttpWebRequest)WebRequest.Create( ResolveUrl("~/default.aspx"));
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
string json = .... //Constrtuct your json here
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
    streamWriter.Write(json);
}
var response = httpWebRequest.GetResponse();
Restuta
  • 5,855
  • 33
  • 44
System Down
  • 6,192
  • 1
  • 30
  • 34
5

This has gotten much easier with WebAPI on the server side (ASP.Net MVC4 Controller if you want to host under IIS; WebAPI self hosted under a separate, standalone app is also possible) and HttpClient on the client side.

http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client

Allan Elder
  • 4,052
  • 17
  • 19