4

I'm having trouble getting routing to work with kestrel.

I can't find any good tutorials on how to implement this inside of a netcore console app.

I want to build a simple web server that will have 2-3 end-points that I can access.

public class WebServer
{
    public static void Init()
    {
        IWebHostBuilder builder = CreateWebHostBuilder(null);
        IWebHost host = builder.Build();
        host.Run();
    }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args)
    {
        var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .Build();

        return WebHost.CreateDefaultBuilder(args)
            .UseUrls("http://*:5000")
            .UseConfiguration(config)
            .UseStartup<Startup>();
    }

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRouting();
            // ????
        }

        public void Configure(IApplicationBuilder app)
        {
            // ????
        }
    }
}
user3953989
  • 1,844
  • 3
  • 25
  • 56
  • Why don't you use the Web API template with `dotnet new webapi`? What you try to do is exactly what that template does - a Console application that creates and starts a Host. Controllers, routing and endpoints are added with calls to `AddControllers`, `UseRouting` and `UseEndpoints`. – Panagiotis Kanavos Jul 11 '19 at 08:34
  • 1
    @PanagiotisKanavos The project is already 99% done and this request was a late addition. I would hate to have to start a new project and move all the code and refactor around it. I was hoping to just add the code needed to my existing project – user3953989 Jul 11 '19 at 11:52
  • And yet, you accepted the answer that shows just that, or rather something that adds the MVC infrastructure, not just the endpoints like WebAPI would do. `MyEndpoint` there is a Controller – Panagiotis Kanavos Jul 11 '19 at 12:23
  • With the accepted answer, I only had to add 2 lines of code to get it working. Your suggestion is to create a new project and that would cause much more work that I would hope. The MVC stuff might be overkill but if you can provide a code example for something better suited that I can easily add to my existing project I'm more than happy to try it out and learn about it. – user3953989 Jul 11 '19 at 21:12

1 Answers1

7

File > New Project > Empty ASP.NET Core application.

In order to run it in a console application, make sure you select the name of you project in the "Run" dropdown in Visual Studio.

enter image description here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;

namespace WebApplication7
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

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

    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddMvc();
        }

        // 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();
        }
    }

    public class MyEndpoint : Controller
    {
        [Route("")]
        public IActionResult Get()
        {
            return new OkResult();
        }
    }
}
bartbje
  • 331
  • 1
  • 7