[英]Code compiles and runs in Visual Studio even though it has the name of a class that isn't referenced
I have an ASP.NET Core web application I'm testing, and it shouldn't be compiling or running, but it does both. 我有一个正在测试的ASP.NET Core Web应用程序,它不应该正在编译或运行,但是两者都可以。 The problem should be that I'm using a class name --
ActionResult
-- from an assembly that isn't referenced. 问题应该是我正在使用未引用的程序
ActionResult
的类名ActionResult
。 Notice how ActionResult
is not blue like a resolved class name, and also notice how there is no compiler error underlined: 请注意,
ActionResult
如何像解析的类名一样不是蓝色,并且还注意下划线时没有编译器错误:
Furthermore the project compiles and runs just fine (although doesn't behave as expected, naturally, which is to respond with HTTP GET requests to that URL with the values seen above). 此外,该项目可以编译并正常运行(尽管自然不会达到预期的效果,这自然是使用上面显示的值响应该URL的HTTP GET请求)。 What is going on?
到底是怎么回事?
Here's all the code just for reference: 这是所有代码,仅供参考:
Program.cs: 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: 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: 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" });
}
}
}
Here's VS and .NET info: 这是VS和.NET信息:
Microsoft Visual Studio Professional 2017
Microsoft Visual Studio专业版2017
Version 15.9.4版本15.9.4
VisualStudio.15.Release/15.9.4+28307.222VisualStudio.15。发行版/15.9.4+28307.222
Microsoft .NET Core 2.1Microsoft .NET Core 2.1
ActionResult
is in the Microsoft.AspNetCore.Mvc
namespace which you are in fact referencing. ActionResult
在您实际上引用的Microsoft.AspNetCore.Mvc
命名空间中。 Visual Studio just isn't highlighting correctly, which is a bug I encounter off and on. Visual Studio不能正确突出显示,这是我反复遇到的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.