2017-02-09 42 views
0

私のコントローラクラスでSystem.Web.Httpライブラリを参照できますが、別のクラスでは参照できないC#プロジェクトがあります。参照はプロジェクト全体に追加され、両方のクラスには必要な指示をすべて使用しています。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?)を参照してください。参照されているアセンブリがこのクラスで使用されていないことを単に指摘しています。

このアセンブリの機能を使用する場合は、問題の警告が表示されなくなります。コメントをもとに

EDIT:必要な機能は、ApiControllerとして、別のクラスのprotectedインタフェースの一部である場合、あなたはあなた自身のクラスでこのような機能にアクセスするには、そのクラスを拡張する必要があります。このような拡張が、参照したアセンブリ内のメソッドを引き続き使用する場合(コンパイラが不要なusingディレクティブを示す場合)、usingディレクティブに関するコンパイラの警告も同様に解決されます。

+0

Requestメソッドが、新しいクラスが継承しないApiControllerクラスの保護されたFunctionであるため、コンパイルに失敗します。 ApiController.Request()は静的関数System.IO.Path()のように使用することはできません。 –

+0

@WalterZAMBOTTI - この場合、アセンブリのヘルプをどのように参照するのですか?保護されたメソッドは、宣言クラスまたは子クラスによってのみアクセスできます。 – CoolBots

0

あなたのAPIコントローラでは、リクエストはHttpRequestMessageタイプで、CreateResponseはHttpRequestMessageの拡張メソッドです。なぜあなたのCSVmgrクラスのHttpResponseMessage新しいインスタンスを作成しないでください。 like:

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); 
関連する問題