我正在测试一个ASP.NET核心web应用程序,它不应该编译或运行,但它可以同时编译或运行。问题
应该是
我用的是一个类名--
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}");
});
}
}
}
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" });
}
}
}
微软Visual Studio专业版2017
版本15.9.4
VisualStudio.15.Release/15.9.4+28307.222
微软.NETCore2.1