John.P
John.P

Reputation: 657

MVC - The resource cannot be found

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

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.

Requested 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();
        }

The File directory structure

Upvotes: 1

Views: 1920

Answers (3)

heymega
heymega

Reputation: 9411

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.

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

Upvotes: 3

Chaitanya Gadkari
Chaitanya Gadkari

Reputation: 2807

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

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

Upvotes: 2

fdomn-m
fdomn-m

Reputation: 28621

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

<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.

Upvotes: 2

Related Questions