English 中文(简体)
在NET6 Console设立网络应用中心 采用404项对策
原标题:Setting up WebApplication host in .NET6 Console App leads to 404 response

I have the below code that runs successfully, receives Http requests but returns 404. I created a .NET Console App and installed Microsoft.AspNetCore.App package. I see the WebApplication is receiving the requests but always responds with 404.

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

using AppHostTest;

var testBuilder = WebApplication.CreateBuilder(new WebApplicationOptions ());
testBuilder.Services.AddRazorPages();
testBuilder.Services.AddTransient<IVehicle, Car>();

var testapp = testBuilder.Build();
testapp.UseHttpsRedirection();
testapp.UseStaticFiles();
testapp.UseRouting();
testapp.UseAuthorization();
testapp.MapRazorPages();
testapp.Run();

尽管上述法典与你在利用网络应用模板把项目混为一谈时所看到的完全相同,但行为并不相同。

任何帮助了解上述安排的错误之处都会受到高度赞赏。

我尝试安装微软。 AspNetCode.Mvc 包裹,在测试应用方面增加AddMvc()服务,使用Mvc(),但并未奏效和行为。

“在座的影像描述”/</a

更新:

我看见通过手法创建的WebApplicationBuilder,把网络服务器作为复合FileProvider作为万维网网路仪,作为wwwroot。

“enterography

whereas, the WebApplicationBuilder created by ConsoleApp populates the WebRootFileProvider as NullFileProvider and WebRootPath as null. enter image description here

问题回答

如果你想设立一个伙伴关系。 NET Core Razor Pages web app from Console App model, You cantries:

In .csproj file, specified and use 微软.NET.Sdk。 网页,如以下。

<Project Sdk="Microsoft.NET.Sdk.Web">

then 增加Program.cs文档中所需的服务和中文本部分,如:<>。

var testBuilder = WebApplication.CreateBuilder(new WebApplicationOptions());
testBuilder.Services.AddRazorPages();
//testBuilder.Services.AddTransient<IVehicle, Car>();

var testapp = testBuilder.Build();
testapp.UseHttpsRedirection();
testapp.UseStaticFiles();
testapp.UseRouting();
testapp.UseAuthorization();
testapp.MapRazorPages();
testapp.Run();

后添加页数和raz页数如下:

“entergraph

Index.cshtml>

@page
@model AppHostTest.Pages.IndexModel
@{
}

<h1>hello world</h1>

测试结果

“在此处的影像描述”/</a





相关问题
Random "Timed out ProduceRequest in flight" messages

I am getting some random timeout errors while publishing messages using Confluent.Kafka. The application runs in a Kubernetes cluster and is built using the .NET 6 framework. When the default timeout (...

如何将.Net 6.0的dll引用到.Net Framework 4.8中

大家好! 我有一个类库项目,目标是.NET Framework 6.0。当我将此DLL引用到目标为.NET Framework 4.8的另一个项目中时,我会收到以下错误消息 ...

热门标签