2015-11-03 170 views
0

我正在做一個MVC教程和我在做一個真正的基本Web應用程序的一部分。MVC Web應用程序錯誤HTTP 404

這是我的控制器文件夾內的控制器:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 

namespace FirstMVCApplication.Controllers 
{ 
    public class HomeContoller : Controller 
    { 
     // 
     // GET: /HomeContoller/ 

     public ActionResult Index() 
     { 
      int hour = DateTime.Now.Hour; 

      ViewBag.Greeting = 
      hour < 12 
      ? "Good Morning. Time is" + DateTime.Now.ToShortTimeString() 
      : "Good Afternoon. Time is " + DateTime.Now.ToShortTimeString(); 

      return View(); 
     } 

    } 
} 

這是我的看法查看文件夾內:

@{ 
    Layout = null; 
} 

<!DOCTYPE html> 

<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>Index</title> 
</head> 
<body> 
    <div> 
     @ViewBag.Greeting (<b>From Index View</b>) 
    </div> 
</body> 
</html> 

進出口使用剃刀。

而當我執行它它返回一個HTTP 404資源未找到。它是一個空Web MVC-4應用程序。

EDIT RouteConfig

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.Web.Routing; 

namespace FirstMVCApplication 
{ 
    public class RouteConfig 
    { 
     public static void RegisterRoutes(RouteCollection routes) 
     { 
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

      routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 
     } 
    } 
} 
+0

此視圖必須保留在Views/Home文件夾中。你應該在瀏覽器中發佈路由配置文件和你正在調試的url。 – Fals

+0

http:// localhost:「Port」/ – Nickso

+0

這裏有兩點:1 - 4XX錯誤,意味着你(用戶,而不是服務器)做錯了什麼。 2- 404 =您請求的資源找不到,可能是您使用的是錯誤的網址。請向我們展示在瀏覽器中顯示的網址 –

回答

0

刪除這個@ {佈局= NULL; }從Layout.cshtml文件,你張貼在這裏

添加@RenderBody()來Layout.cshtml你張貼在這裏

在這你必須在這裏Index.cshtml文件的文件。在頁面頂部添加文件

@ {Layout =「〜/ Views/Shared/Layout.cshtml」; }

+0

我的觀點被稱爲'Index.cshtml',所以你說我寫'@RenderBody()'並刪除'@ {Layout = null; } Index.cshtml文件怎麼樣?我只有一個看法。 你說'/ Views/Shared /'但視圖在'Views'文件夾內,就是這樣。 我是否必須在「Views」文件夾內創建另一個名爲'Shared'的文件夾?另外,我在哪裏編寫'@ {Layout =「〜/ Views/Shared/Layout.cshtml」; }' – Nickso

相關問題