2013-10-02 48 views
0

我有一個通用的處理程序,根據查詢字符串服務PDF的。我以前沒有使用過的處理程序,但是當我建立我得到2個奇怪的錯誤,我無法找到一個解決方案:錯誤是:ASP.NET通用處理程序編譯錯誤

  1. 命名空間不能直接包含成員如字段或 方法

  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" %> 
+0

我可以準確地複製你的代碼(註釋掉兩個' APP_Code'線),它工作正常。此外,如果你看我的[先前發佈]中的示例(http://stackoverflow.com/questions/19123961/filehandler-in-asp-net/19124733#19124733),你不必亂七八糟的查詢字符串和開關語句......爲什麼不直接鏈接到PDF?每次添加新文件時,這條路線都會很痛苦。 – MikeSmithDev

+0

感謝@MikeSmithDev,我將改變這種方式來刪除switch語句,並且只要在測試過程中確定這個文件信息從數據庫中提取出來,它就是一種「概念驗證」解決方案,我只是想確定它在我清理之前工作。它沒有建立,因爲ashx文件因某種原因被設置爲編譯。 –

回答

0

我想通了......本身被設置爲編譯ashx的文件,將其變更爲內容,並只編譯CS文件,並建立..