我遇到了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文件(为简单起见,不包括使用语句):
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>
你和我一样,可能需要安装ASP.NET!
在Windows Server 2012 R2上,可以通过打开或关闭Windows功能来完成此操作:
在您的设置中,UseIISIntegration“干扰”UseUrls,因为UseUrls设置用于Kestrel进程而不是IISExpress / IIS。
如果要更改IIS端口,请查看Properties / launchSettings.json - 您可以在其中配置IIS正在使用的applicationUrl。
您可以删除UseIISIntegration用于测试目的,然后您可以连接到端口5000,但您永远不应该使用Kestrel作为面向Internet的服务器,它应该始终在反向代理(如IIS或Nginx等)后面运行。
有关更多信息,请参阅the Hosting Docs Page。
我正在使用IIS(我不清楚OP是否尝试使用IIS),但我没有安装.NET Core Windows Server Hosting包,如instructions like this one中所述。安装该模块后,我的应用程序服务(即没有500错误)
在vs2017中运行asp.net MVC核心1.1项目也会出现此错误。 通过将所有NuGet Packages版本1.x升级到最新2.x并将目标框架升级到.NET Core 2.1来解决此问题。