
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()); }} 我的问题是“ASP.net的等效方法/设计/新项目是什么?”
我一直把它们写成WebForms,看起来像这样:
首先(基本上是空的).aspx文件:
<%@ Page Language="C#" autoEventWireup="true" CodeBehind="SimpleServlet_Json.aspx.cs" inherits="com.stackoverflow.question.SimpleServlet_Json" %>
然后这个.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; } }} 但我暗自担心我要求我的同事c#程序员采用非直观的风格.
这些示例相当人为,实际上它们被用作数据库实体的JsON表示.例如,URL example.com/product/1是HTML版本(具有与URL关联的JsP或ASPx页面),而example.com/product/1.Json是JsON表示(具有这些类之一).我是URL重写的粉丝.
解决方法 httpHandlers应该在2003年之后的任何版本的.NET版本中执行它.请参阅此 Microsoft article.但是,更现代(和更具创造性)的方法是使用.NET MVC或更具体的.NET Web API.
从他的asp.net博客上的Scott Gu’s 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:”
并且official Microsoft .NET Web API page指出:
总结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.
以上是内存溢出为你收集整理的c# – 在ASP.net项目中,Servlet(在tomcat中扩展HttpServlet的Java类)的等价物是什么?全部内容,希望文章能够帮你解决c# – 在ASP.net项目中,Servlet(在tomcat中扩展HttpServlet的Java类)的等价物是什么?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)