简体   繁体   English

将MVC添加到VS2015中的空ASP.NET 5项目:清空500响应

[英]Adding MVC to an empty ASP.NET 5 project in VS2015: Empty 500 Responses

I'm trying to create a minimal MVC6 (I guess MVC Core now?) project in Visual Studio 2015, by starting with an empty project and adding the MVC bits - I don't need a lot of the extra guff that's added as part of the "Web App" project, and I want to learn by doing, so am trying the following method. 我正在尝试在Visual Studio 2015中创建一个最小的MVC6(我猜MVC Core?)项目,从一个空项目开始并添加MVC位 - 我不需要添加很多额外的guff作为部分“Web App”项目,我想通过实践来学习,所以我尝试以下方法。 Unfortunately every request to this web app results in a 500 internal error response with no further details. 不幸的是,对此Web应用程序的每个请求都会导致500内部错误响应而无需进一步详细信息

  1. Create a new ASP.NET 5 empty web project. 创建一个新的ASP.NET 5空Web项目。 This starts with the "Hello World!" 这从“Hello World!”开始。 response for all requests 响应所有请求
  2. Add the following line to project.json: 将以下行添加到project.json:
 "Microsoft.AspNet.Mvc": "6.0.0-rc1-final", 
  1. Modify the code in Startup.cs as follows: 修改Startup.cs中的代码,如下所示:
 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); } 
  1. Add a Controllers folder with HomeController.cs: 使用HomeController.cs添加Controllers文件夹:
 public class HomeController : Controller { // GET: /<controller>/ public IActionResult Index() { return View(); } } 
  1. Add a Views folder, with _Viewstart.cshtml in the root: 在根目录中添加一个Views文件夹,其中包含_Viewstart.cshtml:
 @{ Layout = "_Layout"; } 
  1. Add Views/Shared folder with _Layout.cshtml (My actual app is an angular2 app so has a lot more static content than this) 使用_Layout.cshtml添加视图/共享文件夹(我的实际应用是一个angular2应用,所以有比这更多的静态内容)
 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>My Test App</title> </head> <body> <div> Testing </div> </body> </html> 
  1. Add Views/Home/Index.cshtml with content: 添加包含内容的Views / Home / Index.cshtml:
 @{ ViewBag.Title = "My Test App"; } <div>Testing</div> 

But when I run this, I get a 500 Internal error response for every request, with no error messages or content that I can see to indicate what might be wrong? 但是当我运行这个时,我得到每个请求的500内部错误响应,没有错误消息或内容我可以看到表明可能出错的地方? Any ideas what may be wrong here? 任何想法在这里可能是错的?

Well, I have my answer to this. 好吧,我有我的答案。 I actually found it as I putting the finalising touchs on the question, so I thought I'd post them if anyone else has similar issues. 事实上,当我对问题进行最终确定时,我发现了它,所以如果其他人有类似的问题,我想我会发布它们。 It seems that if you forget to put a @RenderBody call in _Layout.cshtml, then you get a 500 internal error and nothing obvious that tells you what is wrong! 似乎如果你忘记在_Layout.cshtml中放置一个@RenderBody调用,那么你会得到一个500内部错误,没有任何明显的错误告诉你什么是错的! The fix was just to add the @RenderBody() to _Layout.cshtml: 修复只是将@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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM