简单的静态页面calculator.html:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form action="Handlers/CalculaterHandler.ashx" method="post" >
<input type="text" name="number1"/>+<input type="text" name="number2" />=<input type="text" name="result" />
<input type="submit" name="btnSubmit" value="计算"/>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebDemo.Handlers
{
/// <summary>
/// CalculaterHandler 的摘要说明
/// </summary>
public class CalculaterHandler : IHttpHandler
{
public void Proce***equest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
context.Response.ContentType = "text/html";
string num1 = context.Request.Params["number1"];
string num2 = context.Request.Params["number2"];
int result = Convert.ToInt32(num1) + Convert.ToInt32(num2);
//context.Response.Write(num1 +"+"+num2+"="+result);
string html = @"<!DOCTYPE html ><html xmlns='http://www.w3.org/1999/xhtml'>
<head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<title></title>
</head>
<body>
<form action='Handlers/CalculaterHandler.ashx' method='post' >
<input type='text' name='number1' value='" + num1
+ @"' />+<input type='text' name='number2' value='" + num2 +
@"' />=<input type='text' value='" + result +
@"' />
<input type='submit' name='btnSubmit' value='计算'/>
</form>
</body>
</html>";
context.Response.Write(html);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
注意这两句会造成结果不同:用context.Response.ContentType = "text/plain";
结果就会按原样输出文本.
用 context.Response.ContentType = "text/html";
结果才是正常的HTML格式输出.
需要了解的概念
Content-Type:用于定义用户的浏览器或相关设备如何显示将要加载的数据,或者如何处理将要加载的数据
MIME:MIME类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。
text/html的意思是将文件的content-type设置为text/html的形式,浏览器在获取到这种文件时会自动调用html的解析器对文件进行相应的处理。
text/plain的意思是将文件设置为纯文本的形式,浏览器在获取到这种文件时并不会对其进行处理。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。