1

How do i create a page like an action page in php, in C# ?To be specific I only require a page that contains only methods that handle various operations and return result.

How do i solve the above scenario ?

5
  • You can accomplish that by using a class in the application, and referring to an instance of that class.
    – Tim
    May 13, 2014 at 7:12
  • I think http handlers what you are looking for, you may not need this though.
    – adt
    May 13, 2014 at 7:16
  • From where do you want to call those methods? From other server-side code, or from client-side HTML? May 13, 2014 at 7:33
  • I want to do it on my server side code..
    – Nikhil
    May 14, 2014 at 7:27
  • @user256103 : i need an action page like one in php
    – Nikhil
    May 14, 2014 at 7:30

2 Answers 2

1

Create a class, then inside the class create your methods to construct the logic behind your application.

Then reference this class with the using keyword in your web pages and use the methods you created wherever you like.

0

Read about HttpHandlers.

For example, you want to have a method that returns a json string in a web forms application.

Add a new item to your project of type "Generic Handler". This will create a new .ashx file. The main method of any class that implements IHttpHandler is ProcessRequest.

public void ProcessRequest (HttpContext context) {

    if(String.IsNullOrEmpty(context.Request["day"]))
    {
        context.Response.End(); 
    }

    string json = "";
    byte[] bytes = getByteArray();

    json = JsonConvert.SerializeObject(bytes);
    context.Response.ContentType = "text/json";
    context.Response.Write(json);
}

Change the url in your AJAX call and that should do it. The JavaScript would look like this , where GetFileHandler.ashx is the name of the IHttpHandler you just created:

$.ajax(
    {
        type: "POST",
        async: true,
        url: 'Handlers/GetFileHandler.ashx',
        data: "Day=" + $.toJSON(day),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            console.log("SUCCESS:" + msg);
        },
        error: function (msg) {
            console.log("error:" + msg);
        }
    });

Read more here for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.