通过已发布的WebAPI调用时,C#方法无法正常工作

时间:2014-02-19 11:54:47

标签: c# .net api asp.net-web-api

我已将我的方法简化为测试目的。在VS 2013调试模式下本地运行时,它可以正常工作。

这是我的APIController:

    namespace Citizen.Hub.WebAPI.Controllers
{
    public class ReportsController : ApiController
    {
        [HttpGet]
        public void RunAttendanceByYear(string school, string fromdate, string todate)
        {
            var runreport = new AttendanceReports();

            runreport.WebAPITest(school, fromdate, todate);
        }
    }
}

以下是我的简化测试方法:

public void WebAPITest(string school, string fromdate, string todate)
        {
            //Create new instance of Excel, Workbook and Worksheet
            Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
            Workbook xlWorkbook = xlApp.Workbooks.Add();
            Sheets xlSheets = xlWorkbook.Sheets as Sheets;

            //Create new worksheet for school
            Worksheet xlNewSheet = (Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing);

            //Worksheet headers
            xlNewSheet.Cells[1, "A"] = "School";
            xlNewSheet.Cells[1, "B"] = "From";
            xlNewSheet.Cells[1, "C"] = "To";

            //Populate sheet with data
            xlNewSheet.Cells[2, "A"] = school;
            xlNewSheet.Cells[2, "B"] = fromdate;
            xlNewSheet.Cells[2, "C"] = todate;

            //Define filename and file path
            string fileName = string.Format(@"{0}\Test.xlsx", Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));

            //Save this data as a file
            xlWorkbook.SaveAs(fileName);

            //Quit Excel application
            xlApp.Quit();

            //Release COM objects
            if (xlApp != null)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);

            if (xlWorkbook != null)
                System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkbook);

            xlApp = null;
            xlWorkbook = null;
            GC.Collect();
        }

在调试模式下运行时工作正常,并将“Test.xlsx”输出到桌面。

当我在本地发布WebAPI并为其分配端口号1111时,它只是不想工作。我相信IIS配置正确,因为当我浏览它时,我得到默认的VS WebAPI主页。

我的电话是:http://localhost:1111/api/reports?school=TestSchool&fromdate=2013-09-01&todate=2014-02-07

我在IE中得到'Aborted'结果,Response是HTTP / 1.1 204 No Content

有什么建议吗?感谢。

RouteConfig:

public class RouteConfig 
{ 
    public static void RegisterRoutes(RouteCollection routes) 
    { 
         routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
         routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", 
         defaults: new {controller = "Home", action = "Index", id =UrlParameter.Optional});
    } 
}

1 个答案:

答案 0 :(得分:0)

问题在于IIS和权限。我基本上按照此链接中的步骤操作: http://www.bloing.net/2011/01/how-to-make-iis7-play-nice-with-office-interop/