简体   繁体   English

什么是ASP.net项目中的Servlet(在tomcat中扩展HttpServlet的Java类)的等价物?

[英]What is the equivalent of a Servlet (Java class that extends HttpServlet in tomcat) in an ASP.net project?

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: 我在Apache Tomcat项目开始时开始编写自己的Web应用程序,因此当我编写一个Servlet,用一些小的JSON响应某些GET或POST时,我的代码看起来会接近这个:

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?" 我的问题是“ASP.net的等效方法/设计/新项目是什么?”

I have been writing these as WebForms that look like this: 我一直把它们写成WebForms,看起来像这样:

First the (basically empty) .aspx file: 首先(基本上是空的) .aspx文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SimpleServlet_json.aspx.cs" Inherits="com.stackoverflow.question.SimpleServlet_json" %>

Then this .cs file: 然后这个.cs文件:

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. 但我暗自担心我要求我的同事c#程序员采用非直观的风格。

These examples are rather contrived, in practice they are used as the JSON representation of a database entity. 这些示例相当人为,实际上它们被用作数据库实体的JSON表示。 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). 例如,URL example.com/product/1是HTML版本(具有与URL关联的JSP或ASPx页面),而example.com/product/1.json是JSON表示(具有这些类之一)。 I'm a fan of URL Rewriting. 我是URL重写的粉丝。

HttpHandlers should do it in any version .NET version after 2003. see this Microsoft article . HttpHandlers应该在2003年之后的任何版本的.NET版本中执行它。请参阅此Microsoft文章

However , a more modern (and more intiutive) approach would be to use .NET MVC or more specifically .NET Web API. 但是 ,更现代(更具创造性)的方法是使用.NET MVC或更具体的.NET Web API。

Quote from Scott Gu's blog on his asp.net blog: 在他的asp.net博客上引用Scott Gu的博客:

"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:" “我们新的ASP.NET Web API支持使您能够轻松创建功能强大的Web API,可以从广泛的客户端访问(从使用JavaScript的浏览器到任何移动/客户端平台上的本机应用程序)。它提供以下支持”

And the official Microsoft .NET Web API page states: 官方Microsoft .NET Web API页面指出:

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是一个框架,可以轻松构建可覆盖广泛客户端的HTTP服务,包括浏览器和移动设备。 ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework. ASP.NET Web API是在.NET Framework上构建RESTful应用程序的理想平台。

I think you are looking for a Generic Handler (.ashx) 我认为你正在寻找一个Generic Handler (.ashx)

<%@ WebHandler Language="C#" Class="MyHandler" %>

using System;
using System.Web;

public class MyHandler : IHttpHandler 
{
    public void ProcessRequest (HttpContext ctx) 
    {
        var json = new JSONResonse()
        {
            Success = ctx.Request.QueryString["name"] != null,
            Name = ctx.Request.QueryString["name"]
        };

        ctx.Response.ContentType = "application/json";
        ctx.Response.Write(JsonConvert.SerialzeObject(json));
    }

    public bool IsReusable {
        get { return false; }
    }
}

Make requests to them the same as you would your pages. 向他们发出与您的页面相同的请求。 yoursite.com/my-handler.ashx?name=foo yoursite.com/my-handler.ashx?name=foo

I had a similar question and some research on it ended to Web API. 我有一个类似的问题,一些关于它的研究结束于Web API。 Wanna share with SO. 想与SO分享。

In the MSDN site, http://msdn.microsoft.com/en-us/library/jj823172(v=vs.110).aspx 在MSDN站点中, http://msdn.microsoft.com/en-us/library/jj823172(v = vs.110).aspx

Found a video tutorial where they said that for machine cosumption like iPhone or web app clients of JSON or xml, web API is recommended option. 找到一个视频教程,他们说,对于iPhone或JSON或xml的Web应用程序客户端等机器消耗,建议使用Web API。 Its around the last part of the video. 它围绕视频的最后部分。

While for more complex machine to machine communication WCF is prefereable. 而对于更复杂的机器到机器通信,WCF是优选的。

http://channel9.msdn.com/Series/Building-Web-Apps-with-ASP-NET-Jump-Start/Building-Web-Apps-with-ASPNET-Jump-Start-04-Building-a-Service-Layer-with-ASPNET-Web-API http://channel9.msdn.com/Series/Building-Web-Apps-with-ASP-NET-Jump-Start/Building-Web-Apps-with-ASPNET-Jump-Start-04-Building-a-Service-层与-ASPNET的Web-API

Here is a screenshot from their presentation. 这是他们演示的截图。

在此输入图像描述

I can't really say i can draw parallels between a Servlet and some type on asp.net, as webforms is quite different. 我不能说我可以在Serv.net和asp.net上的某些类型之间绘制相似之处,因为webforms是完全不同的。

From your sample code i can recommend that you take a look at asp.net webapi as this will map quite well to what you are currently trying to do. 从您的示例代码中我可以建议您查看asp.net webapi,因为这将很好地映射到您当前正在尝试的内容。

More like Owin 更像是Owin

'OWIN defines a standard interface between .NET web servers and web applications.' 'OWIN定义了.NET Web服务器和Web应用程序之间的标准接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM