c# - 自托管 ASp.net core 2.2 应用程序,未找到 View 'Index'。搜索了以下位置

标签 c# asp.net-core asp.net-core-mvc asp.net-core-2.0 identityserver4

我正在创建 ASP.NET Core Web 应用程序并通过控制台托管它。

我正在使用IdentitySvr4 Quick Start UI并将所有文件添加到我的项目中。

Console Application

现在,当我通过运行控制台启动服务器并浏览到 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/

相关文章:

c# - ENC1003 C# 在应用程序运行时不会应用在项目中所做的更改

c# - ASP.NET 核心 MVC | System.ArgumentNullException : 'Value cannot be null. ' | Configuration. GetConnectionString

c# - UWP 中的 WebSocket 因未知原因崩溃

c# - SOLID , SRP , I可比

asp.net-core - 在csproj中定义AspNetCoreHostingModel抛出无法启动 'npm'异常

c# - 如何自动更新我用 C# 开发的应用程序?

c# - 如何在 Azure 广告身份验证中停止注销询问您要注销哪个帐户?

c# - Multi-Tenancy 自定义 ILogger - 无法使用来自单例的范围服务

c# - 如何将指定属性的 lambda 表达式转换为表示相同属性的 asp.net mvc 兼容 'name' 字符串?

c# - 系统.Net.WebException : The remote server returned an error: (403) Forbidden when access Google API