rory.ap
rory.ap

Reputation: 35318

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. The problem should be that I'm using a class name -- ActionResult -- from an assembly that isn't referenced. Notice how ActionResult is not blue like a resolved class name, and also notice how there is no compiler error underlined:

enter image description here

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). What is going on?

enter image description here

Here's all the code just for reference:

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" });
        }
    }
}

Here's VS and .NET info:

Microsoft Visual Studio Professional 2017
Version 15.9.4
VisualStudio.15.Release/15.9.4+28307.222
Microsoft .NET Core 2.1

Upvotes: 1

Views: 110

Answers (1)

Valuator
Valuator

Reputation: 3627

ActionResult is in the Microsoft.AspNetCore.Mvc namespace which you are in fact referencing. Visual Studio just isn't highlighting correctly, which is a bug I encounter off and on.

Upvotes: 7

Related Questions