【发布时间】:2019-07-22 15:25:36
【问题描述】:
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 从控制器返回值? (一个简单的json)
【问题讨论】:
-
能否请您至少将代码发布为文本而不是图像?让其他人也可以更轻松地进行测试。
-
@LasseVågsætherKarlsen 它在问题的链接中:jsbin.com/zovicuduto/edit?html。我已将其粘贴为图像,因此人们将看到它在 Linqpad 中的显示方式。如果我粘贴它,控制器的代码会太长。
-
你是如何提出请求的?
-
@ChrisPratt 通过浏览器。 i.imgur.com/3w2GBCX.jpg
标签: c# asp.net-core .net-core linqpad