繁体   English   中英

HTTP 错误 500:本地主机当前无法处理此请求

[英]HTTP Error 500: localhost is currently unable to handle this request

我遇到了 HTPP 错误 500,但不知道为什么。 当我启动我的服务时,我弹出一个 Chrome 浏览器并导航到http://localhost:5000 ,并弹出错误。 Chrome 开发者工具窗口显示了这个错误:

Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:5000/

这是我的 Startup.cs 文件(为简单起见,不包括 using 语句):

namespace Tuner
{
    public class Startup
    {
        public static void Main(string[] args)
        {
            var exePath = Process.GetCurrentProcess().MainModule.FileName;
            var directoryPath = Path.GetDirectoryName(exePath);

                                var host = new WebHostBuilder()
               .CaptureStartupErrors(true)
               .UseKestrel()
               .UseUrls("http://localhost:5000")
               .UseContentRoot(Directory.GetCurrentDirectory())
               .UseIISIntegration()
               .UseStartup<Startup>()
               .Build();
            host.Run();
        }


        public Startup(IHostingEnvironment env)
        {
            //Setup Logger
            Log.Logger = new LoggerConfiguration()
                .WriteTo.Trace()
                .MinimumLevel.Debug()
                .CreateLogger();
            // Set up configuration sources.
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json");
            //.AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; set; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSwaggerGen();

            services.AddMvc().AddJsonOptions(options =>
            {
                options.SerializerSettings.ContractResolver =
                    new CamelCasePropertyNamesContractResolver();
            });
        }

        // 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, IApplicationLifetime lifetime)
        {

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}");
            });


            lifetime.ApplicationStopping.Register(() =>
            {
                Log.Debug("Application Stopping. Do stuff.");
            });
        }
    }
}

使用 MVC,这会导致 HomeController Index 方法被调用:

namespace Tuner.Controllers
{
    public class HomeController : Controller
    {
        public string appVersion = typeof(HomeController).Assembly.GetName().Version.ToString();
        public string appName = "Empty Web App";

        [HttpGet("/")]
        public IActionResult Index()
        {
            var url = Request.Path.Value;
            if (url.EndsWith(".ico") || url.EndsWith(".map"))
            {
                return new StatusCodeResult(404);
            }
            else
            {
                // else block is reached
                return View("~/Views/Home/Index.cshtml");
            }
        }

        public IActionResult Error()
        {
            return View("~/Views/Shared/Error.cshtml");
        }

        [HttpGetAttribute("app/version")]
        public string Version()
        {
            return appVersion;

        }

        [HttpGetAttribute("app/name")]
        public string ProjectName()
        {
            return appName;
        }
    }
}

这是我的 Index.cshtml 文件(已放置在 Views/Home 中):

@{
    ViewBag.Title = "Tuner";
}

@section pageHead {
}

@section scripts {
    <script src="~/vendor.bundle.js"></script> 
    <script src="~/main.bundle.js"></script>

}

<cache vary-by="@Context.Request.Path">
    <app>Loading content...</app>
</cache>

遵循本指南后,我在 IIS 10.0 上托管 ASP.NET Core 3.1 应用程序时出现以下错误:

https://docs.microsoft.com/en-us/aspnet/core/tutorials/publish-to-iis?view=aspnetcore-3.1&tabs=visual-studio

This page isn't working
localhost is currently unable to handle this request.
HTTP ERROR 500

在此处输入图片说明

如果您使用的是 Windows:

启动事件查看器 -> Windows 日志 -> 应用程序。

在此处输入图片说明

在我的情况下,我有下面的错误,可以从那里继续(允许我的应用程序池用户访问 localdb)。

Microsoft.Data.SqlClient.SqlException (0x80131904):建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。 服务器未找到或无法访问。 验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供程序:SQL 网络接口,错误:50 - 发生本地数据库运行时错误。无法创建自动实例。有关错误详细信息,请参阅 Windows 应用程序事件日志。

您和我一样,可能需要安装 ASP.NET!

在 Windows Server 2012 R2 上,这可以通过打开或关闭 Windows 功能来完成:

  1. 完成向导的开始之前安装类型服务器选择步骤。
  2. Server Roles 下,找到Web Server (IIS)节点并展开它。
  3. 展开Web 服务器节点。
  4. 展开应用程序开发节点。
  5. 根据需要检查ASP.NET 3.5ASP.NET 4.5节点。
  6. 完成添加角色和功能向导

在您的设置中,UseIISIntegration “干扰”了 UseUrls,因为 UseUrls 设置用于 Kestrel 进程而不是 IISExpress/IIS。

如果您想更改 IIS 端口,请查看 Properties/launchSettings.json - 在那里您可以配置 IIS 正在使用的 applicationUrl。

您可以删除 UseIISIntegration 以进行测试,然后您可以连接到端口 5000,但您永远不应将 Kestrel 用作面向 Internet 的服务器,它应该始终在反向代理(如 IIS 或 Nginx 等)之后运行。

有关更多信息,请参阅托管文档页面

我正在使用 IIS(我不清楚 OP 是否尝试使用 IIS),但我没有安装 .NET Core Windows Server Hosting bundle如本说明中所述。 安装该模块后,我的应用程序提供服务(即没有 500 错误)

在 vs2017 中运行 asp.net MVC core 1.1 项目也得到这个错误。
通过将所有 NuGet 包版本 1.x 升级到最新的 2.x 并将目标框架升级到 .NET Core 2.1 来解决。

请检查数据库一次。 在我的情况下,我在 asp 样板中遇到了问题。 VS17 .net sdk 2.2 版。 只需更改应用程序设置文件并添加任何工作数据库并尝试再次运行项目。

一个可能的原因是您试图将 PageModel 作为参数传递。 出于某种原因,这失败了:

System.NotSupportedException:不支持“GT.ADS.Web.Pages.Error1Model.TempData”上的集合类型“Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionary”。

如果您尝试从标准 MVC 架构迁移到 Blazor 设置,这应该很常见。 在我从模型中传递了我需要的东西后,错误就解决了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM