我有一個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
.
.
.
}
'Request'是'ApiController'的屬性。你在哪裏期待'CSVmgr'從中獲取? – yaakov
它聲稱System.Web.Http沒有必要,因爲它是。您不使用或繼承來自該命名空間的任何類型。 –
是什麼讓你覺得這是一個錯誤?這是否會導致任何類型的編譯器錯誤(除了關於不必要的'using'指令的警告外)? – CoolBots