2015-11-03 142 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

相关问题