Search code examples
c#asp.net-core.net-corelinqpad

Linqpad 6 (Core) and .Net Core Api?


Linqpad 6 supports .Net Core.
When I create a new empty .Net Core API solution in visual studio , I get a simple template with a simple demo controller.

enter image description here

When I run it in visual studio it uses a command-line server (kestrel) to run the project :

enter image description here

So I wanted to see if I can run this project in Linqpad 6.

So I've installed all nugets and copied the code to Linqpad :

https://i.sstatic.net/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();
    }
}

I do see that it is now listening :

enter image description here

But calls to http://localhost:5000/api/values do get acked , but without the json value response from controller :

enter image description here

Question:

How can I get Linqpad to return the value from the controller? ( a simple json)


Solution

  • There's a difference in the way LINQPad executes code that could account for this not working.

    Being a scripting tool, LINQPad wraps everything in a class (otherwise, the Main method would have nowhere to live). So ValuesController is actually ends up as a nested type, UserQuery.ValuesController, and this could potentially upset the routing API.

    For such situations, LINQPad has the ability to extract all nested types and move them outside UserQuery (using the Roslyn API). To enable this, add the following to the start of your query:

    #LINQPad nonest
    

    Something else to consider is that a default MVC project includes an appsettings.json file. Should this be required for your code in LINQPad, you need to create such a file and add a reference to it (when you reference to a non-binary file, LINQPad copies it into the output folder, which is exactly where appsettings.json needs to be).

    Edit: There's now a checkbox in the Query Properties dialog to add ASP.NET Core references to a query in LINQPad 6. This pulls the assemblies straight from the shared framework folder, and is easier than finding the right NuGet packages.