Asp.net mvc 4 MVC Web应用程序错误HTTP 404
我正在做一个MVC教程,我正在做一个非常基本的web应用程序 这是我的控制器文件夹中的控制器:Asp.net mvc 4 MVC Web应用程序错误HTTP 404,asp.net-mvc-4,c#-4.0,razor,Asp.net Mvc 4,C# 4.0,Razor,我正在做一个MVC教程,我正在做一个非常基本的web应用程序 这是我的控制器文件夹中的控制器: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace FirstMVCApplication.Controllers { public class HomeContoller : Controller {
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.cshtml文件中删除此@{Layout=null;} 将@RenderBody()添加到您在此处发布的Layout.cshtml文件中 在这里的Index.cshtml文件中。在页面文件的顶部添加
@{Layout=“~/Views/Shared/Layout.cshtml”}此视图必须位于Views/Home文件夹中。您应该在浏览器中发布路由配置文件和要设置的url。这里有两点:1-4XX错误,表示您(用户,而不是服务器)做错了什么。2-404=未找到您请求的资源,可能是您使用了错误的url。请向我们显示浏览器中显示的url。它是http://localhost:55171/您的视图应位于
/Views/Home
中,并命名为Index.cshtml
。您可以尝试更显式的路由http://localhost:55171/Home/Index
。您还重新编译、清除了缓存并杀死了系统托盘中的开发web服务器?我的视图名为Index.cshtml
,所以您说我写@RenderBody()
并删除@{Layout=null;}
Index.cshtml文件呢?我只有一种看法。你说的是/Views/Shared/
,但是视图在视图
文件夹中,就是它。我是否必须在我的视图
文件夹中创建另一个名为共享
的文件夹?另外,我在哪里写@{Layout=“~/Views/Shared/Layout.cshtml”}
@{
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>
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 }
);
}
}
}