I started programming my own web applications during the beginning of the Apache Tomcat project, and thus when I am writing a Servlet that responds with some small piece of JSON to some GET or POST my code would look something close to this:
package com.stackoverflow.question;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.json.*;
public class SimpleServlet_json extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
JSONObject json = new JSONObject();
try {
json.put("Success", true);
json.put("Name", request.getParameter("name"));
} catch (JSONException e) {}
response.setContentType("application/json");
response.getOutputStream().print(json.toString());
}
}
My question is "what is the equivalent method/design/New Item for ASP.net?"
I have been writing these as WebForms that look like this:
First the (basically empty) .aspx
file:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SimpleServlet_json.aspx.cs" Inherits="com.stackoverflow.question.SimpleServlet_json" %>
Then this .cs
file:
namespace com.stackoverflow.question
{
public partial class SimpleServlet_json : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var json = new JSONResponse()
{
Success = Request.QueryString["name"] != null,
Name = Request.QueryString["name"]
};
Response.ContentType = "application/json";
Response.Write(JsonConvert.SerializeObject(json));
}
}
[Serializable]
class JSONResponse
{
public string Name { get; set; }
public bool Success { get; set; }
}
}
But I secretly worry that I am asking my fellow c# programmers to adopt a non-intuitive style.
These examples are rather contrived, in practice they are used as the JSON representation of a database entity. For an example the URL example.com/product/1
is the HTML version (with a JSP or ASPx page associated with the URL) while example.com/product/1.json
is the JSON representation (with one of these classes). I'm a fan of URL Rewriting.
HttpHandlers should do it in any version .NET version after 2003. see this Microsoft article.
However, a more modern (and more intiutive) approach would be to use .NET MVC or more specifically .NET Web API.
Quote from Scott Gu's blog on his asp.net blog:
"Our new ASP.NET Web API support enables you to easily create powerful Web APIs that can be accessed from a broad range of clients (ranging from browsers using JavaScript, to native apps on any mobile/client platform). It provides the following support:"
And the official Microsoft .NET Web API page states:
ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With