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

时间:2016-02-10 23:57:23

标签: asp.net-mvc visual-studio-2015 asp.net-core

我试图在Visual Studio 2015中创建一个最小的MVC6(我猜MVC Core?)项目,从一个空项目开始并添加MVC位 - 我不需要很多作为" Web App"的一部分添加的额外guff;项目,我想通过实践来学习,所以我尝试以下方法。不幸的是,对此Web应用程序的每个请求都会导致500内部错误响应而无需进一步详细信息

  1. 创建一个新的ASP.NET 5空Web项目。这开始于" Hello World!"对所有请求的回复
  2. 将以下行添加到project.json:
  3. "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    
    1. 修改Startup.cs中的代码,如下所示:
    2. 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. 使用HomeController.cs添加Controllers文件夹:
      2. public class HomeController : Controller
        {
            // GET: /<controller>/
            public IActionResult Index()
            {
                return View();
            }
        }
        
        1. 在根目录中添加一个Views文件夹,其中包含_Viewstart.cshtml:
        2. @{
              Layout = "_Layout";
          }
          
          1. 使用_Layout.cshtml添加视图/共享文件夹(我的实际应用是一个angular2应用,所以静态内容比此更多)
          2. <!DOCTYPE html>
            
            <html>
            <head>
                <meta name="viewport" content="width=device-width" />
                <title>My Test App</title>
            </head>
            <body>
                <div>
                    Testing
                </div>
            </body>
            </html>
            
            1. 使用内容添加Views / Home / Index.cshtml:
            2. @{
                  ViewBag.Title = "My Test App";
              }
              <div>Testing</div>
              

              但是当我运行这个时,我会为每个请求获得500内部错误响应,没有错误消息或内容我可以看到它可能表示错误?任何想法在这里可能是错的?

1 个答案:

答案 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>