简体   繁体   English

即使它具有未引用的类的名称,代码也可以在Visual Studio中编译并运行

[英]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.222 VisualStudio.15。发行版/15.9.4+28307.222
Microsoft .NET Core 2.1 Microsoft .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.

相关问题 Visual Studio 2010-错误提示即使引用了程序集也找不到类 - Visual Studio 2010 - error saying can't find class even though assembly is referenced Visual Studio 2012-报告“找不到命名空间”用于自动生成的代码,但可以编译并运行 - Visual Studio 2012 - reporting “namespace not found” for autogenerated code but compiles and runs 即使我在测试项目中引用了该类,我也无法在测试类中识别我要测试的类类型的对象 - Object of type class i want to test isn't getting recognized in the test class even though I referenced the class in the test project Visual Studio在未引用库时选择错误的构造函数? - Visual Studio chooses wrong constructor when a library isn't referenced? 即使Visual Studio 2015生成了,也找不到类库 - Visual Studio 2015 can't find class library even though it builds Visual Studio Code 未检测到错误 - Visual Studio Code isn't detecting errors 是否可以通过代码中的名称引用Visual Studio中的嵌入式文件资源? - Can an embedded file resource in Visual Studio be referenced by name in code? 代码在视觉工作室2015上编译但在2013年失败 - code compiles on visual studio 2015 but failing on 2013 Visual Studio 2010创建编译代码的应用程序 - Visual Studio 2010 Creating application that compiles code Visual Studio MVC 5显示错误但编译并运行正常 - Visual Studio MVC 5 shows errors but compiles and runs okay
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM