ASP.Net newbie question (example code appreciated)

Discussion in 'ASP .Net' started by 2b|!2b==?, Mar 29, 2007.

  1. 2b|!2b==?

    2b|!2b==? Guest

    I want to create a really simple (proof of concept) website with the
    following requirements.

    1). I can specify the web address (say http://localhost/mywebsite)
    2). I have a "handler" script (in C#) that allows me to respond to
    requests from a client (see notes below):

    Notes:
    =======
    I have an existing application which consists of a C++ frontend, and a
    PHP backend. I am moving from PHP to ASP.Net at the server side. At the
    server side, I have PHP code somewhat like this:

    <?
    // function definitions here ...

    $enc_str = base64_decode($_POST['request']);
    $xml_str = mcrypt_ecb(MCRYPT_BLOWFISH, ENCRYPTION_KEY, $enc_str,
    MCRYPT_DECRYPT);

    $xml = simplexml_load_string($xml_str);

    if(!is_object($xml))
    respond(INVALID_REQUEST);

    // Determine requested function and invoke handler
    switch($xml->function[0])
    {
    case FUNCTION_1:
    foobar1($xml);
    break;

    case FUNCTION_2:
    foobar2($xml);
    break;

    default:
    respond(INVALID_REQUEST);
    }
    ?>

    Basically (for those not familiar with PHP) it means I can call this url
    (i.e. php script) with some arguments from my C++ code (POST method),
    which then invokes the appropriate function on the server side.

    I Know the following (thanks to Göran Andersson )
    ==================================================
    1). $_POST is equivalent to Request.Form.
    2). base64_decode is equivalent to Convert.FromBase64String
    3). Equivalent decryption may be found in System.Security.Cryptography
    namespace.
    4). I can use an XmlDocument object to handle the XML data.
    5). Normally an ASP.NET page uses the markup in the aspx file to render
    the page, but I can remove everything in the page (except the @page tag)
    and output anything I like from the code behind using Response.Write.

    Given the above points, could someone please post a C# equivalent of the
    PHP snippet above (you may leave out the encryption - if you don't know
    thw equivalent function in C#) - I just need the boilerplate C# code to
    get started, so that I can implement this server side "handler" in the
    website I described.
     
    2b|!2b==?, Mar 29, 2007
    #1
    1. Advertisements

  2. I think, something like this

    protected void Page_Init(object sender, EventArgs e)
    {

    string enc_str =
    System.Text.ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(Request.Form["request"]));
    string xml_str = enc_str; // mcrypt_ecb???

    XmlDocument d = new XmlDocument();
    d.LoadXml(xml_str);

    if (d == null)
    return;

    // depends on structure of your xml, e.g.
    first = d.DocumentElement.FirstChild;

    switch (d)
    {
    case "1":
    ....
    break;
    case "2":
    ....
    break;
    default:
    .... // error
    break;
    }
    }
     
    Alexey Smirnov, Apr 2, 2007
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.