[英]Return HTML view in .NET Core Console application with Kestrel
I've got .Net Core Console application. 我有.Net Core Console应用程序。 I've been trying for awhile to make it possible to run
Process
and listen to port
at the same moment and this was my way of doing it: 我一直在尝试使同时运行
Process
和侦听port
成为可能,这是我的方法:
This is my program.cs
class: 这是我的
program.cs
类:
class Program
{
static void Main(string[] args)
{
var ConsOut = Console.Out;
Console.SetOut(new StreamWriter(Stream.Null));
BuildWebHost(args).Start();
Console.SetOut(ConsOut);
while (true)
{
RunIbit();
}
}
public static IWebHost BuildWebHost(string[] args)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true)
.AddCommandLine(args)
.Build();
var host = new WebHostBuilder()
.UseKestrel()
.UseConfiguration(config)
.UseStartup<Startup>()
.UseIISIntegration()
.Build();
return host;
}
public static void RunIbit()
{
// Execute Proccess.Start();
}}
and here is Startup.cs
: 这是
Startup.cs
:
public class Startup
{
public Startup()
{
var builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
Configuration = builder.Build();
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddLogging();
var serviceProvider = services.BuildServiceProvider();
services.AddSingleton<IConfigurationRoot>(Configuration);
return WindsorRegistrationHelper.CreateServiceProvider(ContainerManager.Container, services);
}
public void Configure(IApplicationBuilder app){
app.Run(context => {
return context.Response.WriteAsync("Hello world");
});
}
}
Since I want to show the results of the process in a web page I've been wondering whether it's possible to do that somehow instead of writing that Hello world
? 由于我想在网页上显示该过程的结果,所以我一直在想,是否有可能以某种方式代替写
Hello world
? Tried adding app.UseMvc()
instead of app.Run(context => { return context.Response.WriteAsync("Hello world"); });
尝试添加
app.UseMvc()
而不是app.Run(context => { return context.Response.WriteAsync("Hello world"); });
but it didn't work with .net core console application and this was the only that worked for me (or at least the one I found) but I couldn't find how to replace that text with html file. 但这不适用于.net核心控制台应用程序,并且这是唯一对我有用(或至少我找到的应用程序),但我找不到如何用html文件替换该文本的方法。 Does anyone know a way?
有人知道吗?
I've been trying for awhile to make it possible to run Process and listen to port at the same moment
我一直在尝试使运行Process和同时监听port成为可能
short answer : 简短答案:
here's a how-to in details : 这是一个详细的操作方法:
I'm not sure whether you want to achieve the goal via MVC solution or static html files , so I provide you with two solutions : 我不确定您是要通过MVC解决方案还是要通过静态html文件实现目标,因此我为您提供了两种解决方案:
Plan A : As I notice that you have said 计划A:正如我所注意到的,您说过
tried adding app.UseMvc() ... but id didn't work
尝试添加app.UseMvc()...但是id无效
let's config a console program with MVC first . 让我们首先使用MVC配置控制台程序。
Plan B : config a console with static files . 方案B:使用静态文件配置控制台。
Just as a reminder , either Plan A or Plan B requires a Microsoft.NET.Sdk.Web
SDK . 提醒一下,计划A或计划B都需要
Microsoft.NET.Sdk.Web
SDK。 So we must reference correct SDK at frist , namely change the project sdk from Microsoft.NET.Sdk
to Microsoft.NET.Sdk.Web
: 因此,我们必须首先引用正确的SDK,即将项目sdk从
Microsoft.NET.Sdk
更改为Microsoft.NET.Sdk.Web
:
<Project Sdk="Microsoft.NET.Sdk.Web">
<!-- ... -->
</Project>
.csproj
file : .csproj
文件中添加软件包引用: <Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>netcoreapp2.0</TargetFramework> </PropertyGroup> <ItemGroup> <Folder Include="wwwroot\\" /> </ItemGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.5" /> </ItemGroup> <ItemGroup> <Content Update="appsettings.json"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> </ItemGroup> </Project>
and then you can configure MVC in your Startup
class: 然后可以在
Startup
类中配置MVC:
public void ConfigureServices(IServiceCollection services) { // ... services.AddMvc(); // add mvc services here // ... } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseMvcWithDefaultRoute(); // use mvc here app.Run(context => { return context.Response.WriteAsync("Hello world"); }); }
At last , you can add controllers and views file as you need . 最后,您可以根据需要添加控制器和视图文件。 For instance , if you add a HelloController with a Index action , and the coresponding view file is :
例如,如果添加带有Index操作的HelloController,并且对应的视图文件为:
@{ ViewData["Title"] = "Index"; @ {ViewData [“ Title”] =“索引”; }
}
it woooooooooooooooooooorks 它woooooooooooooooooooorks
you will get a response as expected when you access the URL /hello
tes-console-with-mvc 访问URL
/hello
tes-console-with-mvc时,您将得到预期的响应
you can simply add a wwwroot
directory in your project and register a middleware with UseStaticFiles()
in Configure()
method: 您只需在项目中添加
wwwroot
目录,然后在Configure()
方法中向UseStaticFiles()
注册中间件:
public void Configure(IApplicationBuilder app, IHostingEnvironment env){
// ...
// app.UseMvcWithDefaultRoute();
app.UseStaticFiles(); // add a StaticFiles middleware here
app.Run(async context => {
await context.Response.WriteAsync("Hello,world");
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.