ashx显示xml

ashx的使用:参考文章:http://www.cnblogs.com/zgqys1980/archive/2008/03/11/1100863.html

下面是实际碰到的例子:用户输入地址,根据web.config配置的可访问IP判断是否合法,以及根据地址栏参数判断请求类型,显示不同的xml文件(效果就像直接在IE打开xml文件一样)。

代码
public void ProcessRequest(HttpContext context)
{
    
#region 数据判断(略)
    
//读取Web.config的IP配置,地址栏参数(请求类型)
    
//配置为空、IP地址非法、参数错误码时的提示
    
//由参数得到请求的xml文件名fileName
    #endregion

    
#region 输出XML
    context.Response.ContentType 
= "text/xml";
    
string filePath = CommonCls.ResourcePath + fileName;  //文件全路径
    try
    {
        
using (StreamReader reader = new StreamReader(filePath, System.Text.Encoding.GetEncoding("utf-8")))
        {
            context.Response.Write(reader.ReadToEnd());
        }
    }
    
catch (Exception exp)
    {
        context.Response.Write(exp.Message);
    }
    
#endregion
}
//其它函数(略)
原文地址:https://www.cnblogs.com/vipcjob/p/1639774.html