温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • C#开发web程序中关于 一般处理程序中的context.Response.ContentType = "text/plain"

C#开发web程序中关于 一般处理程序中的context.Response.ContentType = "text/plain"

发布时间:2020-07-22 17:06:02 来源:网络 阅读:12240 作者:warhax 栏目:开发技术
简单的静态页面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>

加上一般处理程序CalculatorHandler.ashx:

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格式输出.

text/html & text/plain的区别

需要了解的概念

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

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

 

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

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


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI