我有一个正在测试的ASP.NET Core Web应用程序,它不应该正在编译或运行,但是两者都可以。问题应该是我正在使用未引用的程序集中的类名ActionResult
。请注意,ActionResult
不会像已解析的类名那样是蓝色的,并且还请注意下划线时没有编译器错误:
此外,该项目可以编译并正常运行(尽管自然不能按预期方式运行,即使用上面显示的值通过HTTP GET请求对该URL进行响应)。发生了什么事?
以下所有代码仅供参考:
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 Professional 2017
版本15.9.4
VisualStudio.15.Release / 15.9.4 + 28307.222
Microsoft .NET Core 2.1
答案 0 :(得分:7)
ActionResult
在您实际上引用的Microsoft.AspNetCore.Mvc
名称空间中。 Visual Studio不能正确突出显示,这是我经常遇到的错误。