【发布时间】:2013-10-08 23:36:08
【问题描述】:
我有一个根据查询字符串为 PDF 提供服务的通用处理程序。我以前没有使用过处理程序,但是当我构建时出现 2 个奇怪的错误,我找不到解决方案:错误是:
命名空间不能直接包含成员,例如字段或 方法
关键字、标识符或字符串需要逐字逐句 说明符:@
这里是代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Center
{
/// <summary>
/// This tracks user opens serves the proper PDF by querystring value f
/// </summary>
public class GetFile : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//Get file reference
if (context.Request.QueryString["f"] != null)
{
string file;
string fullFilePath;
string fileName;
file = context.Request.QueryString["f"];
switch (file)
{
case "Access":
App_Code.bi.LogFileDownload("Access Fact Sheet", context.Session["UserID"].ToString());
fullFilePath = "files/Access2013.pdf#zoom=100";
fileName = "Access2013.pdf";
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
context.Response.TransmitFile(fullFilePath);
context.Response.End();
break;
case "MobileApp":
fullFilePath = "files/mobile_app.pdf#zoom=100";
fileName = "mobile_app.pdf";
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
context.Response.TransmitFile(fullFilePath);
context.Response.End();
break;
case "BillingFact":
fullFilePath = "files/billing_factsheet.pdf#zoom=100";
fileName = "billing_factsheet.pdf";
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
context.Response.TransmitFile(fullFilePath);
context.Response.End();
break;
case "HistoricalFact":
fullFilePath = "files/Implementation.pdf#zoom=100";
fileName = "Implementation.pdf";
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
context.Response.TransmitFile(fullFilePath);
context.Response.End();
break;
case "ImplementationKit":
App_Code.bi.LogFileDownload("Implementation Kit", context.Session["UserID"].ToString());
fullFilePath = "files/Implementation_kit.pdf#zoom=100";
fileName = "Implementation.pdf";
context.Response.Clear();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
context.Response.TransmitFile(fullFilePath);
context.Response.End();
break;
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
ASHX file
---------------
<%@ WebHandler Language="C#" CodeBehind="GetFile.ashx.cs" Class="Center.GetFile" %>
【问题讨论】:
-
我可以准确地复制您的代码(注释掉两个
APP_Code行),它工作正常。此外,如果您在我的previous post 中查看我的示例,您不必搞乱查询字符串和 switch 语句……为什么不直接链接到 PDF?每次添加新文件时,这条路线都会很痛苦。 -
感谢@MikeSmithDev,我将对此进行更改以删除 switch 语句,并在测试过程中完成后从数据库中提取文件信息,这是一种“概念证明”解决方案我只是想在清理它之前确定它是否有效。它没有构建,因为 ashx 文件由于某种原因被设置为编译。