.net HTTP 错误 500:本地主机当前无法处理此请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41992280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP Error 500: localhost is currently unable to handle this request
提问by Roka545
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:
我遇到了 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/
Here is my Startup.cs file (exluding using statements for simplicity):
这是我的 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.");
});
}
}
}
With MVC, this causes the HomeController Index method to get called:
使用 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;
}
}
}
and here is my Index.cshtml file (which has been placed in Views/Home):
这是我的 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>
回答by Kenny Evitt
You, like me, might need to install ASP.NET!
您和我一样,可能需要安装 ASP.NET!
On Windows Server 2012 R2 this can be done via the Turn Windows features on or offfeature:
在 Windows Server 2012 R2 上,这可以通过打开或关闭 Windows 功能来完成:
- Complete the Before You Begin, Installation Type, and Server Selectionsteps of the wizard.
- Under Server Roles, find the Web Server (IIS)node and expand it.
- Expand the Web Servernode.
- Expand the Application Developmentnode.
- Check the ASP.NET 3.5or ASP.NET 4.5nodes, as appropriate.
- Finish the Add Roles and Features Wizard.
- 完成向导的开始之前、安装类型和服务器选择步骤。
- 在Server Roles 下,找到Web Server (IIS)节点并展开它。
- 展开Web 服务器节点。
- 展开应用程序开发节点。
- 根据需要检查ASP.NET 3.5或ASP.NET 4.5节点。
- 完成添加角色和功能向导。
回答by Fionn
In your setup the UseIISIntegration "interferes" with UseUrls, as the UseUrls setting is for the Kestrel process and not the IISExpress/IIS.
在您的设置中,UseIISIntegration “干扰”了 UseUrls,因为 UseUrls 设置用于 Kestrel 进程而不是 IISExpress/IIS。
If you want to change the IIS Port, have a look at Properties/launchSettings.json - there you can configure the applicationUrl IIS is using.
如果您想更改 IIS 端口,请查看 Properties/launchSettings.json - 在那里您可以配置 IIS 正在使用的 applicationUrl。
You could remove the UseIISIntegration for testing purposes and then you can connect to Port 5000, but you nevershould use Kestrel as Internet facing Server, it should always be run behind a Reverse Proxy like IIS or Nginx, etc.
您可以出于测试目的删除 UseIISIntegration,然后您可以连接到端口 5000,但您永远不应将 Kestrel 用作面向 Internet 的服务器,它应该始终在反向代理(如 IIS 或 Nginx 等)后面运行。
See the Hosting Docs Pagefor more information.
有关更多信息,请参阅托管文档页面。
回答by The Red Pea
I was using IIS (I'm unclear if OP was trying to use IIS), but I did not Install the .NET Core Windows Server Hosting bundle, as described in instructions like this one. After installing that Module, my app served (i.e. no 500 error)
我正在使用 IIS(我不清楚 OP 是否尝试使用 IIS),但我没有安装 .NET Core Windows Server Hosting bundle,如本说明中所述。安装该模块后,我的应用程序提供服务(即没有 500 错误)
回答by Song
Run asp.net MVC core 1.1 project in vs2017 also get this error.
Solved by upgrade all NuGet Packages version 1.x to latest 2.x and target framework to .NET Core 2.1.
在 vs2017 中运行 asp.net MVC core 1.1 项目也得到这个错误。
通过将所有 NuGet 包版本 1.x 升级到最新的 2.x 并将目标框架升级到 .NET Core 2.1 来解决。
回答by Kajal sanchela
kindly check database once. In my condition I got issue in asp boilerplate. VS17 .net sdk version 2.2 . just change in app setting file and add any working database and try to run project again.
请检查数据库一次。在我的情况下,我在 asp 样板中遇到了问题。VS17 .net sdk 2.2 版。只需更改应用程序设置文件并添加任何工作数据库并尝试再次运行项目。

