MVC 页面未显示,未找到 404
我有一个非常简单的 MVC 站点,在尝试从一开始加载页面时返回 404 未找到错误。我正在寻找解决此问题的方向,因为错误消息中确实没有任何内容。
更新:问题似乎是由我通过右键单击文件并说设置为起始页来设置起始页引起的。这导致 Visual Studio 尝试直接加载该页面。当使用路由规则修改 url 以访问页面时,页面将按照下面 Keltex 的建议正确加载。
我收到的错误是:
描述:HTTP 404。您正在查找的资源(或其其中之一) 依赖项)可能已被删除,其名称已更改,或者是暂时的 不可用。请检查以下 URL 并确保其拼写正确 正确。
请求的 URL:/Views/Other/Index.aspx
下面我包含了各个部分的代码,路由规则是默认的:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
该站点正在使用嵌套母版页,不确定这是否与问题有关,但尝试包含尽可能多的详细信息尽可能。
我有:
控制器
- 其他控制器
视图:
- 共享文件夹:
- 网站管理员
- 其他文件夹:
- 其他.大师
- 索引.aspx
Site.Master 代码:
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
</title>
</head>
<body>
<div>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
</body>
</html>
Other.Master 代码:
<%@ Master Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewMasterPage" %>
<asp:Content ID="OtherTitle" ContentPlaceHolderID="TitleContent" runat="server">
OTHER PAGE - MASTER TITLE
<asp:ContentPlaceHolder ID="OtherPageTitle" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ID="OtherContent" ContentPlaceHolderID="MainContent" runat="server">
Some other content.
<asp:ContentPlaceHolder ID="PageContent" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>
Index.aspx 代码:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Other/Other.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="IndexTitle" ContentPlaceHolderID="OtherTitle" runat="server">
Home
</asp:Content>
<asp:Content ID="IndexContent" ContentPlaceHolderID="OtherContent" runat="server">
Index content
</asp:Content>
OtherController代码
namespace MVCProject.Controllers
{
public class OtherController : Controller
{
//
// GET: /Member/
public ActionResult Index()
{
// Have also tried:
// return View("Index", "Other.Master");
return View();
}
}
}
I have a very simple MVC site that is returning a 404 not found error when trying to load a page at the very beginning. I'm looking for some direction to troubleshoot this problem since there is really nothing to go on from the error message.
UPDATE: The problem appears to have been cause by me setting the start page by right-clicking on the file and saying set as start page. This caused Visual Studio to try to load that page directly. When modifying the url to access the page using the routing rules the page will load correctly as suggested by Keltex below.
The error I'm getting is:
Description: HTTP 404. The resource you are looking for (or one of its
dependencies) could have been removed, had its name changed, or is temporarily
unavailable. Please review the following URL and make sure that it is spelled
correctly.Requested URL: /Views/Other/Index.aspx
Below I have included the code for the various pieces, routing rules are default:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
The site is using nested MasterPages, not sure if this is involved with the problem but trying to include as much detail as possible.
I have:
Controllers
- OtherController
Views:
- Shared Folder:
- Site.Master
- Other Folder:
- Other.Master
- Index.aspx
Site.Master Code:
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
</title>
</head>
<body>
<div>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
</body>
</html>
Other.Master Code:
<%@ Master Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewMasterPage" %>
<asp:Content ID="OtherTitle" ContentPlaceHolderID="TitleContent" runat="server">
OTHER PAGE - MASTER TITLE
<asp:ContentPlaceHolder ID="OtherPageTitle" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ID="OtherContent" ContentPlaceHolderID="MainContent" runat="server">
Some other content.
<asp:ContentPlaceHolder ID="PageContent" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>
Index.aspx Code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Other/Other.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="IndexTitle" ContentPlaceHolderID="OtherTitle" runat="server">
Home
</asp:Content>
<asp:Content ID="IndexContent" ContentPlaceHolderID="OtherContent" runat="server">
Index content
</asp:Content>
OtherController Code
namespace MVCProject.Controllers
{
public class OtherController : Controller
{
//
// GET: /Member/
public ActionResult Index()
{
// Have also tried:
// return View("Index", "Other.Master");
return View();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为 URL 应该是:
not
URL 通常不以
/Views
为前缀。这只是视图所在的文件夹。通常也不会指定查看Index
,因为这是默认操作。最后,MVC 中通常不指定扩展名.aspx
。如果您希望此页面作为站点的默认页面,您需要将路由规则更改为如下所示:(注意默认控制器从
Home
更改为Other)
I think the URL should be:
not
The URLs are not typically prefixed with
/Views
. This is just the folder in which views are located. Also typically the ViewIndex
is not specified since this is the default action. Finally the extension.aspx
is usually not specified in MVC. If you want this page to come up as the site's default page, you need to change your routing rules to look like this:(notice the change of the default controller from
Home
toOther
)“项目属性”对话框中是否指定了任何特殊的 URL 或路由?
这必须是路由或 URL,而不是 .aspx 页面。
Is there any special URL or route specified in the Project Properties dialog?
This needs to be a route or URL, and not an .aspx page.
404 消息似乎表明您正在尝试直接在 Web 浏览器中访问视图。这是正确的吗?您不应该访问 url /Other/Index 吗?
The 404 message seems to suggest you are trying to access the view directly in the web browser. Is this correct? Shouldn't you be accessing the url /Other/Index?