![](/img/trans.png)
[英]Visual Studio 2010 - error saying can't find class even though assembly is referenced
[英]Code compiles and runs in Visual Studio even though it has the name of a class that isn't referenced
我有一個正在測試的ASP.NET Core Web應用程序,它不應該正在編譯或運行,但是兩者都可以。 問題應該是我正在使用未引用的程序ActionResult
的類名ActionResult
。 請注意, ActionResult
如何像解析的類名一樣不是藍色,並且還注意下划線時沒有編譯器錯誤:
此外,該項目可以編譯並正常運行(盡管自然不會達到預期的效果,這自然是使用上面顯示的值響應該URL的HTTP GET請求)。 到底是怎么回事?
這是所有代碼,僅供參考:
Program.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace WebApplication2
{
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
}
Startup.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace WebApplication2
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "api/{Default}");
});
}
}
}
DefaultController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace WebApplication2.wwwroot
{
[Route("api/[controller]")]
[ApiController]
public class DefaultController : ControllerBase
{
// GET: api/Default
[HttpGet]
public async Task<ActionResult<IEnumerable<string>>> Get()
{
return Task.FromResult(new string[] { "value1", "value2" });
}
}
}
這是VS和.NET信息:
Microsoft Visual Studio專業版2017
版本15.9.4
VisualStudio.15。發行版/15.9.4+28307.222
Microsoft .NET Core 2.1
ActionResult
在您實際上引用的Microsoft.AspNetCore.Mvc
命名空間中。 Visual Studio不能正確突出顯示,這是我反復遇到的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.