如果您想同时避免 WebForms,那么您可以使用 Handler。
我可以理解您为什么要在学习时这样做,但请记住 WebForms 绝对不是敌人。
<%@ WebHandler Language="C#" Class="MyNamespace.Handler" %>
using System.Web;
using System.Data.SqlClient;
namespace MyNamespace
{
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
context.Response.Write("<html><body>");
string connectionString = ""; // todo: fill in your connection string
int numThings;
using (var connection = new SqlConnection(connectionString))
using (var query = new SqlCommand("select count(*) from Lights", connection))
{
numThings = (int)query.ExecuteScalar();
}
context.Response.Write("<h1>There are {0} lights!</h1>");
context.Response.Write("</body></html>");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}