Linqpad 6(核心)和 .Net 核心 Api?

Linqpad 6 (Core) and .Net Core Api?

Linqpad 6 支持.Net Core。
当我在 visual studio 中创建一个新的空 .Net Core API 解决方案时,我得到了一个带有简单演示控制器的简单模板。

当我 运行 它在 visual studio 中时,它使用命令行服务器 (kestrel) 到 运行 项目:

所以我想看看我是否可以在 Linqpad 6 中 运行 这个项目。

所以我已经安装了所有 nugets 并将代码复制到 Linqpad:

https://i.stack.imgur.com/lwRyU.png

void Main()
{

    CreateWebHostBuilder(new string[] { "" }).Build().Run();

}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();

[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{

    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        return new string[] { "value1", "value2" };
    }

}


public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        //if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
    }
}

我确实看到它正在收听:

但是对 http://localhost:5000/api/values 的调用确实会被确认,但是没有来自控制器的 json 值响应:

问题:

如何让 Linqpad 获得来自控制器的 return 值? (一个简单的json)

LINQPad 执行代码的方式不同,可能会导致此问题无法正常工作。

作为脚本工具,LINQPad 将所有内容包装在 class 中(否则,Main 方法将无处可存)。所以 ValuesController 实际上以嵌套类型 UserQuery.ValuesController 结束,这可能会扰乱路由 API.

对于这种情况,LINQPad 能够提取所有嵌套类型并将它们移到 UserQuery 之外(使用 Roslyn API)。要启用此功能,请将以下内容添加到查询的开头:

#LINQPad nonest

另外需要考虑的是默认 MVC 项目包含一个 appsettings.json 文件。如果您在 LINQPad 中的代码需要这样做,您需要创建这样一个文件并添加对其的引用(当您引用 non-binary 文件时,LINQPad 将其复制到输出文件夹中,这正是 appsettings.json 需要)。

编辑:There's now a checkbox 查询属性 对话框中添加 ASP.NET 对 LINQPad 6 中查询的核心引用。这直接从共享框架文件夹,比找到正确的 NuGet 包更容易。