2017-02-09 37 views
0

我有一個C#項目,在我的控制器類中可以引用System.Web.Http庫,但在另一個類中它不能。該參考已被添加到整個項目中,並且這兩個類都具有所有必要的使用指令。System.Web.Http程序集不能在同一個c#項目中全部可見

System.Web.Http的Request方法在某些情況下無法解析?

這裏有兩個類的代碼片段:

控制器/ FormsController.cs

using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Data.Entity.Infrastructure; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Threading.Tasks; 
using System.Web; 
using System.Web.Http; 
using System.Web.Http.Description; 
using FormsImport.Models; 

namespace FormsImport.Controllers 
{ 
    public class TCSTasksController : ApiController 
    { 

     [Route("api/TCSUploadFile")] 
     [AllowAnonymous] 
     public async Task<HttpResponseMessage> UploadCSVFile() 
     { 
      try 
      { 
       var httpRequest = HttpContext.Current.Request; 

       foreach (string file in httpRequest.Files) 
       { 
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created); // <-- the name Request does exists 
. 
. 
. 
} 

CSVmanager.cs

using System; 
using System.Collections.Generic; 
using System.Data.Entity; 
using System.Data.Entity.Infrastructure; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Threading.Tasks; 
using System.Web; 
using System.Web.Http; //<-- Compiler claims this directive is unneccesary 
using System.Web.Http.Description; 
using FormsImport.Models; 


namespace FormsImport 
{ 
    public class CSVmgr 
    { 

     public async Task<HttpResponseMessage> UploadCSVFile() 
     { 
      try 
      { 
       var httpRequest = HttpContext.Current.Request; 

       foreach (string file in httpRequest.Files) 
       { 
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created); // <-- the name Request Does not exist in the current context 

. 
. 
. 
} 
+0

'Request'是'ApiController'的屬性。你在哪裏期待'CSVmgr'從中獲取? – yaakov

+0

它聲稱System.Web.Http沒有必要,因爲它是。您不使用或繼承來自該命名空間的任何類型。 –

+0

是什麼讓你覺得這是一個錯誤?這是否會導致任何類型的編譯器錯誤(除了關於不必要的'using'指令的警告外)? – CoolBots

回答

0

我想你應該有你的編譯器/ IDE同意( Visual Studio?)在這種情況下 - 它只是指出引用的程序集沒有在這個類中使用。

如果您決定使用此程序集的任何功能,則有問題的警告將消失。

編輯基於評論:如果您需要的功能是另一個類的protected接口的一部分,如ApiController,必須擴展這個類在自己的類來訪問這些功能。如果此類擴展隨後使用您引用的程序集中的方法(並且編譯器顯示不必要的using指令),則編譯器有關using指令的警告也將同樣解決。

+0

由於Request方法是我的新類不繼承的ApiController類的受保護函數,因此無法編譯。 ApiController.Request()不能像使用靜態函數System.IO.Path()那樣使用。 –

+0

@WalterZAMBOTTI - 在這種情況下如何引用程序集幫助?受保護的方法只能由聲明類或子類訪問。 – CoolBots

0

在你的api控制器中,Request是一個HttpRequestMessage類型,而CreateResponse是HttpRequestMessage的擴展方法。爲什麼不在你的CSVmgr類中創建一個HttpResponseMessage新實例。像:

HttpResponseMessage response = new HttpResponseMessage(); 
response.StatusCode = HttpStatusCode.Created; 
+0

謝謝。這是最簡單的解決方案! –

0

codran 17解釋說它是最好的。

我的新類不是從APIController類派生的,並且該類的Request方法不是一個靜態函數,我可以像System.IO.Path()那樣調用它。所以我必須使用另一個返回相同結果的類的靜態函數,或者將對APIController的引用傳遞給我的新類。

像這樣:

public async Task<HttpResponseMessage> UploadCSVFile(ApiController controller) 
{ 
    Dictionary<string, object> dict = new Dictionary<string, object>(); 

    try 
    { 
     var httpRequest = HttpContext.Current.Request; 

     foreach (string file in httpRequest.Files) 
     { 
      HttpResponseMessage response = controller.Request.CreateResponse(HttpStatusCode.Created); 
相關問題