我遇到了 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): 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.
关于.net - HTTP 错误 500 : localhost is currently unable to handle this request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41992280/