HTTP 错误 500:本地主机当前无法处理此请求 [英] HTTP Error 500: localhost is currently unable to handle this request

查看:57
本文介绍了HTTP 错误 500:本地主机当前无法处理此请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

加载资源失败:服务器响应状态为 500(内部服务器错误)http://localhost:5000/

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

命名空间调谐器{公开课启动{public static void Main(string[] args){var exePath = Process.GetCurrentProcess().MainModule.FileName;var directoryPath = Path.GetDirectoryName(exePath);var host = new WebHostBuilder().CaptureStartupErrors(真).UseKestrel().UseUrls("http://localhost:5000").UseContentRoot(Directory.GetCurrentDirectory()).UseIISIntegration().UseStartup<启动>().建造();主机.运行();}公共启动(IHostingEnvironment env){//设置记录器Log.Logger = new LoggerConfiguration().WriteTo.Trace().MinimumLevel.Debug().CreateLogger();//设置配置源.var builder = new ConfigurationBuilder().SetBasePath(env.ContentRootPath).AddJsonFile("appsettings.json");//.AddEnvironmentVariables();配置 = builder.Build();}公共 IConfigurationRoot 配置 { 获取;放;}//这个方法被运行时调用.使用此方法向容器添加服务.public void ConfigureServices(IServiceCollection 服务){服务.AddSwaggerGen();services.AddMvc().AddJsonOptions(options =>{options.SerializerSettings.ContractResolver =新的 CamelCasePropertyNamesContractResolver();});}//这个方法被运行时调用.使用此方法配置 HTTP 请求管道.公共无效配置(IApplicationBuilder 应用程序,IHostingEnvironment 环境,ILoggerFactory loggerFactory,IApplicationLifetime 生命周期){如果 (env.IsDevelopment()){app.UseDeveloperExceptionPage();}别的{app.UseExceptionHandler("/Home/Error");}app.UseStaticFiles();app.UseMvc(routes =>{路线.MapRoute(名称:默认",模板:{controller=Home}/{action=Index}");});life.ApplicationStopping.Register(() =>{Log.Debug("应用程序停止.做些事情.");});}}}

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

命名空间 Tuner.Controllers{公共类 HomeController : 控制器{public string appVersion = typeof(HomeController).Assembly.GetName().Version.ToString();public string appName = "Empty Web App";[HttpGet("/")]公共 IActionResult 索引(){var url = Request.Path.Value;if (url.EndsWith(".ico") || url.EndsWith(".map")){返回新的 StatusCodeResult(404);}别的{//到达 else 块return View("~/Views/Home/Index.cshtml");}}公共 IActionResult 错误(){return View("~/Views/Shared/Error.cshtml");}[HttpGetAttribute("应用程序/版本")]公共字符串版本(){返回应用程序版本;}[HttpGetAttribute("应用程序/名称")]公共字符串项目名称(){返回应用程序名称;}}}

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

@{ViewBag.Title = "调谐器";}@section pageHead {}@section 脚本 {<script src="~/vendor.bundle.js"></script><script src="~/main.bundle.js"></script>}<缓存变化=@Context.Request.Path"><app>正在加载内容...</app></缓存>

解决方案

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

如果您使用的是 Windows:

启动事件查看器 ->Windows 日志 ->申请.

就我而言,我遇到了以下错误,可以从那里继续(允许我的应用程序池用户访问 localdb).

<块引用>

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

I'm running into an HTPP Error 500 and I'm not sure why. When I start my service, I pop open a Chrome browser and navigate to http://localhost:5000, and the error pops up. The Chrome Developer Tools windows shows this single error:

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

Here is my Startup.cs file (exluding using statements for simplicity):

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.");
            });
        }
    }
}

With MVC, this causes the HomeController Index method to get called:

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;
        }
    }
}

and here is my Index.cshtml file (which has been placed in 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>

解决方案

I got the following error hosing an ASP.NET Core 3.1 application on IIS 10.0 after following this guide:

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

If you are on Windows:

Start Event Viewer -> Windows Logs -> Application.

In my case I had the error below and could continue from there (Allow my Application Pool User to access localdb).

Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 -Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.

这篇关于HTTP 错误 500:本地主机当前无法处理此请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆