我正在创建 ASP.NET Core Web 应用程序并通过控制台托管它。
我正在使用IdentitySvr4 Quick Start UI并将所有文件添加到我的项目中。
现在,当我通过运行控制台启动服务器并浏览到 http://localhost:44322/ 时,我收到错误,
InvalidOperationException: The view 'Index' was not found. The following locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml
/Pages/Shared/Index.cshtml
我已将此项目创建为控制台应用程序,并在此过程中添加了所有核心内容。
网络服务器启动并监听端口,但不知何故 View 引擎无法理解。
主程序:
class Program
{
static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args).UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseUrls("http://*:44322")
.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)
{
var signingCertificate = new X509Certificate2(Helpers.GetCertificate());
services.AddIdentityServer()
.AddSigningCredential(new X509Certificate2(signingCertificate))
.AddTestUsers(InMemoryConfiguration.Users().ToList())
.AddInMemoryClients(InMemoryConfiguration.Clients().ToList())
.AddInMemoryApiResources(InMemoryConfiguration.ApiResources());
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, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
app.UseDeveloperExceptionPage();
app.UseIdentityServer();
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
}
}
最佳答案
通常我不会回答我的问题,而只是添加评论,但我认为这会为开发人员节省很多精力和痛苦。
很简单,
我用 Notepad++ 编辑了 .csproj 并找到了以下设置:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="IdentityServer4" Version="2.3.2" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
</ItemGroup>
</Project>
引用一个webproj后,只需将根目录从"Microsoft.NET.Sdk"
更改为webproj即可。 , 至"Microsoft.NET.Sdk.Web"
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="IdentityServer4" Version="2.3.2" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
</ItemGroup>
</Project>
就是这样。不确定为什么所有应用程序逻辑都取决于项目类型(因为它可以托管在任何地方),但工作起来就像一个魅力。
关于c# - 自托管 ASp.net core 2.2 应用程序,未找到 View 'Index'。搜索了以下位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54820782/