[英]How to run ASP.NET Core Kestrel server without logging and config files?
我需要在现有的单片应用程序中注入一个 ASP.NET Core web API 服务器。 我的主要问题是关闭所有配置文件、环境配置、日志记录等等。
我只需要一个带有 MVC 路由的纯代码控制的 HTTP 服务器。
我怎样才能做到这一点?
查看我的要点以获得一个非常小的红隼实例/asp.net web 应用程序。
要启用 MVC,请将其更改如下:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace WebApp
{
public class Program
{
public static async Task<int> Main(string[] args) {
try {
var host = new WebHostBuilder()
.UseKestrel()
.UseUrls("http://localhost:5000/;https://localhost:5001")
.ConfigureServices(_configureServices)
.Configure(_configure)
.Build();
await host.RunAsync();
return 0;
}
catch {
return -1;
}
}
static Action<IServiceCollection> _configureServices = (services) => {
services.AddControllersWithViews();
};
static Action<IApplicationBuilder> _configure = app => {
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.Run((ctx) => ctx.Response.WriteAsync("Page not found."));
};
}
}
这也利用了入口点Main(string[] args)
的新异步功能,将服务器启动包装在 try/catch 中,并注册一个 catch-all not found 处理程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.