HTTP错误500:localhost当前无法处理此请求

Rok*_*545 7 .net asp.net-mvc visual-studio .net-core

我遇到了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/
Run Code Online (Sandbox Code Playgroud)

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

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.");
            });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

使用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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

Ogg*_*las 15

遵循本指南后,我在 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
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

如果您使用的是 Windows:

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

在此处输入图片说明

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

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


Ken*_*itt 8

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

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

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


Fio*_*onn 4

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

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

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

请参阅托管文档页面了解更多信息。