ASP.NET 一般处理程序

一般处理程序

返回的数据

 新建一般处理程序文件,产生Handler1.ashx和Handler1.ashx.cs两个文件。context.Request.HttpMethod

public class Handler1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");//返回字符串

            String filename = context.Server.MapPath(context.Request.FilePath);
            if( context.Request.UrlReferrer.Host==null)
            {
                context.Response.ContentType = "image/jpeg";
                context.Response.WriteFile("/err.jpg");//返回图片
            }
            else
            {
} }
public bool IsReusable { get { return false; } } }

返回json

JavaScriptSerializer jss = new JavaScriptSerializer();
string json = jss.Serialize(p); 
context.Response.ContentType = "application/json";
context.Response.Write(json);

 判断是post或get方法 if (HttpContext.Current.Request.HttpMethod.ToUpper() == "POST")

取get和post的参数

context.Request.Params["id"]

 String getp1=context.Request.QueryString["aa"].ToString();
String post2=context.Request.Form["aa"].ToString();

WebForm文件(Web窗体)

 新建WebForm文件(Web窗体),产生WebForm1.aspx,WebForm1.aspx.cs,WebForm1.aspx.designer.cs

 WebForm1.aspx文件中直接写html,开头有<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MTT.WebForm1" %>的引用说明。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MTT.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <p>hello</p>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>
View Code

有 Page_Load事件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MTT
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
View Code

             this.Session["state"] = 1;
            this.Cache["state"] = 1;
            this.Application["state"]=1;
            if (Request["action"] == "PostEwm")

asp.net webform取得get和post参数

String getp1=Request.QueryString["aa"].ToString();
String post2=Request.Form["aa"].ToString();

webform的缺点是 view和controller是放在一起的,WebForm一出现后,随之而来的是大量的组件诞生,<asp:button>组件,当然可以用html的组件。

ASP.NET处理程序

 产生文件名IISHandler2.cs一个文件,有ProcessRequest,直接对get、post请求处理,类似一般处理程序。

using System;
using System.Web;

namespace MTT
{
    public class IISHandler1 : IHttpHandler
    {
        /// <summary>
        /// 您将需要在网站的 Web.config 文件中配置此处理程序
        /// 并向 IIS 注册它,然后才能使用它。有关详细信息,
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序,则返回 false。
            // 如果按请求保留某些状态信息,则通常这将为 false。
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            //在此处写入您的处理程序实现。
            context.Request.Params
        }

        #endregion
    }
}
View Code

 MVC架构

新建,ASP.NET Web应用程序>选择MVC

 Views:login.cshtml;

 Model:class类

controlers:

 Asp.net Web开发方式,分为两种:Web 窗体MVC,MVC是将请求交给控制器处理,而WebForm是将请求交给请求页的后台文件(.cs文件的Page_Load)处理,MVC前后端分的更细。

MVC与Web 窗体的区别参考

http://blog.csdn.net/yisuowushinian/article/details/17646121

原文地址:https://www.cnblogs.com/cb168/p/5301703.html