简体   繁体   中英

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. Unfortunately every request to this web app results in a 500 internal error response with no further details.

  1. Create a new ASP.NET 5 empty web project. This starts with the "Hello World!" response for all requests
  2. Add the following line to project.json:
 "Microsoft.AspNet.Mvc": "6.0.0-rc1-final", 
  1. Modify the code in Startup.cs as follows:
 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:
 public class HomeController : Controller { // GET: /<controller>/ public IActionResult Index() { return View(); } } 
  1. Add a Views folder, with _Viewstart.cshtml in the root:
 @{ 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)
 <!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:
 @{ 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? 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! The fix was just to add the @RenderBody() to _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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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