我试图在Visual Studio 2015中创建一个最小的MVC6(我猜MVC Core?)项目,从一个空项目开始并添加MVC位 - 我不需要很多作为" Web App"的一部分添加的额外guff;项目,我想通过实践来学习,所以我尝试以下方法。不幸的是,对此Web应用程序的每个请求都会导致500内部错误响应而无需进一步详细信息
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(); // ADDED } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app) { app.UseIISPlatformHandler(); // REMOVED // app.Run(async (context) => // { // await context.Response.WriteAsync("Hello World!"); // }); app.UseMvc(config => { config.MapRoute("Default", "{controller}/{action}/{id?}", new { controller = "Home", action = "Index" }); }); } // Entry point for the application. public static void Main(string[] args) => WebApplication.Run<Startup>(args); }
public class HomeController : Controller { // GET: /<controller>/ public IActionResult Index() { return View(); } }
@{ Layout = "_Layout"; }
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>My Test App</title> </head> <body> <div> Testing </div> </body> </html>
@{ ViewBag.Title = "My Test App"; } <div>Testing</div>
但是当我运行这个时,我会为每个请求获得500内部错误响应,没有错误消息或内容我可以看到它可能表示错误?任何想法在这里可能是错的?
答案 0 :(得分:2)
好吧,我有答案。实际上,当我对这个问题进行最终确定时,我发现了它,所以我想如果其他人有类似的问题,我会发布它们。似乎如果你忘记在_Layout.cshtml中放置一个@RenderBody调用,那么你会得到一个500内部错误,没有任何明显的错误告诉你什么是错的!修复只是将@RenderBody()添加到_Layout.cshtml:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>My Test App</title>
</head>
<body>
<div>
@RenderBody()
</div>
</body>
</html>