MVC - 无法找到资源

时间:2022-10-31 21:10:42

I'm working on a MVC project and every time I try to click on "About" or "Contact" I get:

我正在研究一个MVC项目,每当我尝试点击“关于”或“联系”时,我得到:

Description: HTTP 404. The resource you are looking for (or one of the resource dependencies) could have been removed, the name may have changed, or is temporarily unavailable. Check the spelling of the URL below is correct.

说明:HTTP 404.您要查找的资源(或其中一个资源依赖项)可能已被删除,名称可能已更改或暂时不可用。检查以下网址的拼写是否正确。

Requested URL: /Views/Home/Contact.cshtml

请求的URL:/Views/Home/Contact.cshtml

I am able to get to the startpage(index) but once I try to redirect to anoter page like "About" or "Contact" I get the error message as I mentioned above.

我能够到达起始页(索引)但是一旦我尝试重定向到“关于”或“联系人”这样的联系页面,我会收到上面提到的错误消息。

Here is my code:

这是我的代码:

   <ul id="nav">
            <li><a href="~/Views/Home/Index.cshtml">Home</a></li>
            <li><a href="~/Views/Home/About.cshtml">About</a></li>
            <li><a href="~/Views/Home/Contact.cshtml">Contact</a></li>
        </ul>

My HomeController:

  public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

MVC  - 无法找到资源

3 个解决方案

#1


You shouldn't be linking to cshtml files. These files are meant to be rendered by the view engine. I believe you should be linking to your controller's actions which return your views.

你不应该链接到cshtml文件。这些文件旨在由视图引擎呈现。我相信你应该链接到你的控制器回复你的观点的行动。

<ul id="nav">
    <li>@Html.ActionLink("Home", "Index", "Home")</li>
    <li>@Html.ActionLink("About", "About", "Home")</li>
    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>

#2


Remove the .cshtml and /view/ from the links:

从链接中删除.cshtml和/ view /:

<ul id="nav">
    <li><a href="~/Home/Index">Home</a></li>
    <li><a href="~/Home/About">About</a></li>
    <li><a href="~/Home/Contact">Contact</a></li>
</ul>

In MVC your URLs point to actions not pages.

在MVC中,您的URL指向操作而非页面。

#3


You must have been working with web forms(.aspx) pages. MVC does not work this way. As in code you have specified 3 actions in HomeController. default MVC route is /{controller}/{Action}/{other param} so your code will become

您必须使用Web表单(.aspx)页面。 MVC不是这样工作的。在代码中,您已在HomeController中指定了3个操作。默认MVC路由是/ {controller} / {Action} / {other param},因此您的代码将成为

<ul id="nav">
        <li><a href="/Home/Index">Home</a></li>
        <li><a href="/Home/About">About</a></li>
        <li><a href="/Home/Contact">Contact</a></li>
    </ul>

#1


You shouldn't be linking to cshtml files. These files are meant to be rendered by the view engine. I believe you should be linking to your controller's actions which return your views.

你不应该链接到cshtml文件。这些文件旨在由视图引擎呈现。我相信你应该链接到你的控制器回复你的观点的行动。

<ul id="nav">
    <li>@Html.ActionLink("Home", "Index", "Home")</li>
    <li>@Html.ActionLink("About", "About", "Home")</li>
    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
</ul>

#2


Remove the .cshtml and /view/ from the links:

从链接中删除.cshtml和/ view /:

<ul id="nav">
    <li><a href="~/Home/Index">Home</a></li>
    <li><a href="~/Home/About">About</a></li>
    <li><a href="~/Home/Contact">Contact</a></li>
</ul>

In MVC your URLs point to actions not pages.

在MVC中,您的URL指向操作而非页面。

#3


You must have been working with web forms(.aspx) pages. MVC does not work this way. As in code you have specified 3 actions in HomeController. default MVC route is /{controller}/{Action}/{other param} so your code will become

您必须使用Web表单(.aspx)页面。 MVC不是这样工作的。在代码中,您已在HomeController中指定了3个操作。默认MVC路由是/ {controller} / {Action} / {other param},因此您的代码将成为

<ul id="nav">
        <li><a href="/Home/Index">Home</a></li>
        <li><a href="/Home/About">About</a></li>
        <li><a href="/Home/Contact">Contact</a></li>
    </ul>