【问题标题】:Adding MVC to an empty ASP.NET 5 project in VS2015: Empty 500 Responses在 VS2015 中将 MVC 添加到空的 ASP.NET 5 项目:Empty 500 Responses
【发布时间】:2016-02-11 07:58:34
【问题描述】:

我正在尝试在 Visual Studio 2015 中创建一个最小的 MVC6(我猜是 MVC 核心?)项目,从一个空项目开始并添加 MVC 位 - 我不需要很多额外的花言巧语添加为“Web App”项目的一部分,我想边做边学,所以正在尝试以下方法。不幸的是,对这个 Web 应用程序的每个请求都会导致 500 内部错误响应,没有进一步的细节。

  1. 创建一个新的 ASP.NET 5 空 Web 项目。这从“Hello World!”开始。响应所有请求
  2. 将以下行添加到 project.json:
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
  1. 修改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. 使用 HomeController.cs 添加一个 Controllers 文件夹:
public class HomeController : Controller
{
    // GET: /<controller>/
    public IActionResult Index()
    {
        return View();
    }
}
  1. 添加一个 Views 文件夹,根目录中包含 _Viewstart.cshtml:
@{
    Layout = "_Layout";
}
  1. 使用 _Layout.cshtml 添加 Views/Shared 文件夹(我的实际应用是 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. 添加 Views/Home/Index.cshtml 的内容:
@{
    ViewBag.Title = "My Test App";
}
<div>Testing</div>

但是当我运行它时,我会为每个请求得到一个 500 内部错误响应,而我看不到任何错误消息或内容来指示可能有什么问题?任何想法这里可能有什么问题?

【问题讨论】:

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


    【解决方案1】:

    嗯,我有我的答案。实际上,当我对问题进行最后的修改时,我发现了它,所以我想如果其他人有类似的问题,我会发布它们。似乎如果您忘记在 _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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-04
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      相关资源
      最近更新 更多