'ASP.NET MVC visual studio http 404 error on local host, The resource cannot be found.

I'm fairly new to C# programming in Visual Studio 2017 and I am having issue with building my view page on local host. getting below error.

Server Error in '/' Application.

The resource cannot be found. 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/Random.cshtml

the home page loads on local host, it's the view page that does not. below what I have done so far and still not resolved the issue.

  1. In control pane > programmes > Turn Windows features on or off. I have ticked all boxes under "internet Information Services" (IIS 6)

  2. in my Visual Studio project > properties - changed the port number, different start url with, tried with local IIS and none of these changes worked

I've done few other things I can't remember now. I have searched for solution to this on web and tried everything that's related to my issue but none worked. spent over 7 hours so far trying to fix this

I have followed an online course, entered codes exactly same as my course tutor and it works fine for tutor but not for me.

here's my controller coding:

Models page:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Vidly.Models
{
    public class Movie
    {
        public int Id { get; set; }
        public string Name { get; set; }


    }
}

Controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Vidly.Models;

namespace Vidly.Controllers
{
    public class MoviesController : Controller
    {
        // GET: Movies
        public ActionResult Random()
        {
            var movie = new Movie() { Name = "Shrek!" };

            return View(movie);
        }
    }
}

view page (random):
@model Vidly.Models.Movie
@{
    ViewBag.Title = "Random";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>@Model.Name</h2>


Solution 1:[1]

What kind of project did you start with? In one of your config files you should see code defining your route template. Have you tried adding a debug point in the method to see if it's being hit when you navigate to /movies/random ?

routeTemplate: "/{controller}/{id}"
//or
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");

If you selected the wrong base project your path could be different. i.e. http://localhost:49210/api/Movies. In which case the route template

You could also try

    // GET: Movies
    [HttpGet]
    public ActionResult Random()
    {
        var movie = new Movie() { Name = "Shrek!" };

        return View(movie);
    }

Solution 2:[2]

I got the same error when I tried to follow the tutorial. And I think here's where you miss one step. Instead of right clicking the VIEW folder to create the Random.csthml you should expand first the VIEW folder and right click on the Movies folder and store there the Random.cshtml file. Hope this helps

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alex
Solution 2 NLD