简单的静态页面calculator.html:

<!DOCTYPEhtml><htmlxmlns="http://www.w3.org/1999/xhtml"><head><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><title></title></head><body><formaction="Handlers/CalculaterHandler.ashx"method="post"><inputtype="text"name="number1"/>+<inputtype="text"name="number2"/>=<inputtype="text"name="result"/><inputtype="submit"name="btnSubmit"value="计算"/></form></body></html>加上一般处理程序CalculatorHandler.ashx:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;namespaceWebDemo.Handlers{///<summary>///CalculaterHandler的摘要说明///</summary>publicclassCalculaterHandler:IHttpHandler{publicvoidProce***equest(HttpContextcontext){//context.Response.ContentType="text/plain";context.Response.ContentType="text/html";stringnum1=context.Request.Params["number1"];stringnum2=context.Request.Params["number2"];intresult=Convert.ToInt32(num1)+Convert.ToInt32(num2);//context.Response.Write(num1+"+"+num2+"="+result);stringhtml=@"<!DOCTYPEhtml><htmlxmlns='http://www.w3.org/1999/xhtml'><head><metahttp-equiv='Content-Type'content='text/html;charset=utf-8'/><title></title></head><body><formaction='Handlers/CalculaterHandler.ashx'method='post'><inputtype='text'name='number1'value='"+num1+@"'/>+<inputtype='text'name='number2'value='"+num2+@"'/>=<inputtype='text'value='"+result+@"'/><inputtype='submit'name='btnSubmit'value='计算'/></form></body></html>";context.Response.Write(html);}publicboolIsReusable{get{returnfalse;}}}}

注意这两句会造成结果不同:用context.Response.ContentType="text/plain";结果就会按原样输出文本.用context.Response.ContentType="text/html";结果才是正常的HTML格式输出.text/html & text/plain的区别

需要了解的概念

  Content-Type:用于定义用户的浏览器或相关设备如何显示将要加载的数据,或者如何处理将要加载的数据

  MIME:MIME类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。

text/html的意思是将文件的content-type设置为text/html的形式,浏览器在获取到这种文件时会自动调用html的解析器对文件进行相应的处理。

text/plain的意思是将文件设置为纯文本的形式,浏览器在获取到这种文件时并不会对其进行处理。