gpt4 book ai didi

c# - MVC 页面未显示,404 未找到

转载 作者:太空宇宙 更新时间:2023-11-03 20:41:13 29 4
gpt4 key购买 nike

我有一个非常简单的 MVC 站点,在一开始尝试加载页面时返回 404 未找到错误。我正在寻找一些方向来解决此问题,因为错误消息实际上没有任何意义。

更新:问题似乎是我通过右键单击文件并说设置为起始页来设置起始页引起的。这导致 Visual Studio 尝试直接加载该页面。当修改 url 以使用路由规则访问页面时,页面将按照下面 Keltex 的建议正确加载。

我得到的错误是:

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

下面我包含了各个部分的代码,路由规则是默认的:

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);

该网站正在使用嵌套的 MasterPages,不确定这是否与问题有关,但尝试包含尽可能多的细节。

我有:

Controller

  • 其他 Controller

观点:

  • 共享文件夹:
    • 站点管理员
  • 其他文件夹:
    • 其他.硕士
    • 索引.aspx

站点主代码:

<%@ 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>

其他.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代码:

<%@ 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>

其他 Controller 代码

namespace MVCProject.Controllers
{
public class OtherController : Controller
{
//
// GET: /Member/

public ActionResult Index()
{
// Have also tried:
// return View("Index", "Other.Master");

return View();
}
}
}

最佳答案

我认为 URL 应该是:

/Other

不是

/Views/Other/Index.aspx

URL 通常不以 /Views 为前缀。这只是 View 所在的文件夹。此外,通常不会指定 View Index,因为这是默认操作。最后,扩展名 .aspx 通常不会在 MVC 中指定。如果您希望此页面作为站点的默认页面出现,您需要将路由规则更改为如下所示:

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Other", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);

(注意默认 Controller 从 Home 更改为 Other)

关于c# - MVC 页面未显示,404 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2556728/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com