Koalemos Koalemos - 2 months ago 11
ASP.NET (C#) Question

ASP.NET MVC - Server error in '/' Application

I could use some help with this.


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: /Start/Movie/1


I really can't find where the issue is.

This is what I got so far:

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

namespace IMDB.Controller
{
public class StartController : Controller
{
// GET: Start
public ActionResult Index()
{
//ViewBag.Title = DateTime.Now();

var movies = GetMoviesFromRepository();

return View(movies);
}
// GET: Details

public ActionResult Movies(int id)
{
var allMovies = GetMoviesFromRepository();
var movie = allMovies.FirstOrDefault(x => x.MovieID.Equals(id));

return View(movie);
}

private List<Movie> GetMoviesFromRepository()
{

var movies = new List<Movie>();

var movie1 = new Movie();
movie1.MovieID = 1;
movie1.Title = "Terminator";
movie1.Genre = "Action";
movie1.Year = 1984;
movie1.Country = "America";
movies.Add(movie1);

var movie2 = new Movie();
movie2.MovieID = 2;
movie2.Title = "Terminator II";
movie2.Genre = "Action";
movie2.Year = 1991;
movie2.Country = "America";
movies.Add(movie2);

return movies;
}
}
}


Movie.cshtml:

@model IMDB.Models.Movie

@{
ViewBag.Title = "Movies";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Details for @Model.Title</h2>
Title: @Model.Title<br />
Genre: @Model.Genre<br />
Year: @Model.Year<br />
Country: @Model.Country<br />


Index.cshtml:

@model List<IMDB.Models.Movie>

@{
ViewBag.Title = "Start";
}

<ul>
@foreach (var movie in Model)
{
<li>@Html.ActionLink(movie.Title, "Movie", new { id = movie.MovieID })</li>
}


</ul>


RouteConfig.cs:

using System.Web.Mvc;
using System.Web.Routing;

namespace IMDB
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Start",
action = "Index",
id = UrlParameter.Optional
}
);
}
}
}


I am probably just really blind and tired now. What am I missing?

Answer

public ActionResult Movies(int id)

should be

public ActionResult Movie(int id)