Startup.cs in a self-hosted .NET Core Console Application

后端 未结 7 887
情书的邮戳
情书的邮戳 2020-12-07 09:53

I have a self-hosted .NET Core Console Application.

The web shows examples for ASP.NET Core but i do not have a webserver. Just a simple c

7条回答
  •  温柔的废话
    2020-12-07 10:06

    You can resolve it easier. Just make 2 files, and edit Main method:

    Startup.cs

    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    (...)
     public class Startup
        {
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddMvc();
            }
    
            public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
            {
                loggerFactory.AddConsole();
                loggerFactory.AddDebug();
    
    
                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller}/{action}");
                });
            }
        }
    

    YourService.cs

    using Microsoft.AspNetCore.Mvc;
    (...)
    public class MyeasynetworkController : Controller
        {
            [HttpGet]
            public IActionResult Run()
            {
    
                return Ok("Im Working");
            }
        }
    

    and edit your static void Main(string[] args)

                var host = new WebHostBuilder()
                    .UseKestrel()
                    .UseUrls("http://+:5000")
                    .UseStartup()
                    .Build();
    
                host.Run();
    

    then open http://localhost:5000/Myeasynetwork/Run and you should see http status 200 response with plain text Im Working

提交回复
热议问题