-1
我簡化了測試目的的方法。在VS 2013調試模式下本地運行時,它工作正常。當通過發佈的WebAPI調用C#方法不工作
這裏是我的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);
}
}
}
這裏是我的測試簡化的方法:當在調試模式下,輸出 「Test.xlsx」 桌面跑
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();
}
工作正常。
當我在本地發佈WebAPI併爲其分配端口號1111時,它只是不想工作。我相信IIS配置正確,因爲當我瀏覽它時,我得到了默認的VS WebAPI主頁。
我的電話是:http://localhost:1111/api/reports?school=TestSchool&fromdate=2013-09-01&todate=2014-02-07
我得到一個 '中止' 結果在IE和響應是HTTP/1.1 204無內容
有什麼建議?謝謝。
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});
}
}
您是否嘗試給瀏覽器提供一種響應類型JSon只是說它已經完成了,所以瀏覽器看到了一些迴應? –
你能告訴我們路由配置嗎? – Cybermaxs
@PaulZahra如果我將控制器方法更改爲公共字符串並返回「ok」,它會將其作爲jSON返回並且結果爲200,但我的runreport.WebAPITest()方法仍然不起作用。 – user1612185