locked
How to Receive unsolicited posts in ASP.NET Web Form App RRS feed

  • Question

  • User-78157263 posted

    I have a Web form app that I need a page to receive unsolicited posts that may occur at any time. These are JSON data that is sent by by a server that sends events that were subscribed to. Is there a way to do this within the existing project? Since there is no "form" involved I just need a page with code behind to get the JSON and process it. The only response to the post would be a 200 to indicate that is was received or a 400 if the data was not in the correct format.

    I'm not sure how to go about this since none of the ASP plumbing for the app would seem to apply. I would like it to be part of the existing app rather than a new project.

    Any assistance would be greatly appreciated.

    Wednesday, February 17, 2021 9:33 PM

Answers

  • User475983607 posted

    I have a Web form app that I need a page to receive unsolicited posts that may occur at any time. These are JSON data that is sent by by a server that sends events that were subscribed to. Is there a way to do this within the existing project? Since there is no "form" involved I just need a page with code behind to get the JSON and process it. The only response to the post would be a 200 to indicate that is was received or a 400 if the data was not in the correct format.

    I'm not sure how to go about this since none of the ASP plumbing for the app would seem to apply. I would like it to be part of the existing app rather than a new project.

    Any assistance would be greatly appreciated.

    Just create a new Web Form.  Process the JSON in the Page_Load event.

    using Newtonsoft.Json;
    using System;
    using System.IO;
    
    namespace WebFormsDemo.Json
    {
        public class TestDataRequest
        {
            public int Id { get; set; }
            public string message { get; set; }
        }
    
        public class TestDataResponse
        {
            public string message { get; set; }
        }
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if(Request.HttpMethod == "POST")
                {
                    string json;
                    using (var reader = new StreamReader(Request.InputStream))
                    {
                        json = reader.ReadToEnd();
                    }
                    var data = JsonConvert.DeserializeObject<TestDataRequest>(json);
    
                    TestDataResponse response = new TestDataResponse() {
                        message = data.message + " Hello from TestDataResponse"
                    };
    
                    Response.Clear();
                    Response.ContentType = "application/josn";
                    Response.Write(JsonConvert.SerializeObject(response));
                    Response.Flush();
                }
    
            }
        }
    }

    Remove any HTML from the markup page.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebFormsDemo.Json.Default" %>

    https://www.newtonsoft.com/json/help/html/DeserializeObject.htm

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 18, 2021 12:18 AM

All replies

  • User475983607 posted

    I have a Web form app that I need a page to receive unsolicited posts that may occur at any time. These are JSON data that is sent by by a server that sends events that were subscribed to. Is there a way to do this within the existing project? Since there is no "form" involved I just need a page with code behind to get the JSON and process it. The only response to the post would be a 200 to indicate that is was received or a 400 if the data was not in the correct format.

    I'm not sure how to go about this since none of the ASP plumbing for the app would seem to apply. I would like it to be part of the existing app rather than a new project.

    Any assistance would be greatly appreciated.

    Just create a new Web Form.  Process the JSON in the Page_Load event.

    using Newtonsoft.Json;
    using System;
    using System.IO;
    
    namespace WebFormsDemo.Json
    {
        public class TestDataRequest
        {
            public int Id { get; set; }
            public string message { get; set; }
        }
    
        public class TestDataResponse
        {
            public string message { get; set; }
        }
        public partial class Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if(Request.HttpMethod == "POST")
                {
                    string json;
                    using (var reader = new StreamReader(Request.InputStream))
                    {
                        json = reader.ReadToEnd();
                    }
                    var data = JsonConvert.DeserializeObject<TestDataRequest>(json);
    
                    TestDataResponse response = new TestDataResponse() {
                        message = data.message + " Hello from TestDataResponse"
                    };
    
                    Response.Clear();
                    Response.ContentType = "application/josn";
                    Response.Write(JsonConvert.SerializeObject(response));
                    Response.Flush();
                }
    
            }
        }
    }

    Remove any HTML from the markup page.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebFormsDemo.Json.Default" %>

    https://www.newtonsoft.com/json/help/html/DeserializeObject.htm

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 18, 2021 12:18 AM
  • User1120430333 posted

    No Web form app/no Web page is going to receive unsolicited inbound traffic over HTTP. You could use an ASP.NET WebAPI service that can do it that has no security implemented in it listening on a port. And you open the firewall port the WebAPI service is listening on. By opening the firewall port, that allows all unsolicited inbound traffic to reach whatever Web service program that's listening on the port, and it will process it if it client whatever it is uses the appropriate URL and sends the data the listening WebAPI service expects. The Web server will send back a 404 if the URL is wrong, you can send back a 200 if processing is successful, some other code if the processing is not successful 4xx to 5xx pick one and some other code if the Json data is not correct to begin with, a 400 I guess.

    You can create a new WebAPI project within the existing Visual Stuido solution. Trying to get an  existing ASP.NET Webform project, a page,  is not viable IMHO. 

     

    Thursday, February 18, 2021 12:29 AM