自托管 ASp.net 核心 2.2 应用程序,未找到视图 'Index'。搜索了以下位置
Self hosted ASp.net core 2.2 app, The view 'Index' was not found. The following locations were searched
我正在创建 ASP.NET 核心 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
我已将此项目创建为控制台应用程序,并在此过程中添加了所有 nugets。
网络服务器启动并在端口上侦听,但不知何故视图引擎无法理解。
主程序:
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>();
}
启动class:
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"
更改为 "Microsoft.NET.Sdk.Web"
即可将其更改为 webproj
<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>
就是这样。不确定为什么所有的应用程序逻辑都将取决于项目类型(因为它可以托管在任何地方),但工作起来很有魅力。
对我来说,错误出现在我的 .csproj 文件中。 ItemGroup 内容不包括 Index.cshtml。我删除了这个引用,现在我可以导航到 Index.cshtml.
<pre><code>
<ItemGroup>
<Content Remove="Views\Employee\Delete.cshtml" />
<Content Remove="Views\Employee\Index.cshtml" />
</ItemGroup>
</code></pre>
我编辑了 .csproj
只需删除它(如果存在)
"<RazorCompileOnBuild>false</RazorCompileOnBuild>"
"<RazorCompileOnPublish>false</RazorCompileOnPublish>"
来自 <PropertyGroup>
默认情况下 <PropertyGroup>
中找不到“RazorCompileOnBuild”和“RazorCompileOnPublish”
dotnet clean
为我修复了它 (.NETCore 6)
我正在创建 ASP.NET 核心 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
我已将此项目创建为控制台应用程序,并在此过程中添加了所有 nugets。
网络服务器启动并在端口上侦听,但不知何故视图引擎无法理解。
主程序:
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>();
}
启动class:
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"
更改为 "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>
就是这样。不确定为什么所有的应用程序逻辑都将取决于项目类型(因为它可以托管在任何地方),但工作起来很有魅力。
对我来说,错误出现在我的 .csproj 文件中。 ItemGroup 内容不包括 Index.cshtml。我删除了这个引用,现在我可以导航到 Index.cshtml.
<pre><code>
<ItemGroup>
<Content Remove="Views\Employee\Delete.cshtml" />
<Content Remove="Views\Employee\Index.cshtml" />
</ItemGroup>
</code></pre>
我编辑了 .csproj
只需删除它(如果存在)
"<RazorCompileOnBuild>false</RazorCompileOnBuild>"
"<RazorCompileOnPublish>false</RazorCompileOnPublish>"
来自 <PropertyGroup>
默认情况下 <PropertyGroup>
dotnet clean
为我修复了它 (.NETCore 6)