0

I understand that this general question has been asked and answered on here many times. Initially, this problem occurred for all of my web pages. I probably tried 15 different suggested fixes on here. Eventually using a response to a similar question, I ticked the 'Override application root URL' and now my homepage and the about page load correctly. However, the problem persists for my 'Movies' and 'Ratings' pages which are almost the same, which means it's most likely the code. I am obviously missing something and I'm very new to using c#, so I would greatly any help!

This is the 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: /Movies

Here is my Movies controller:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Description;
using AllMovies.DAL;
using AllMovies.Models;

namespace AllMovies.Controllers
{
    public class MoviesController : ApiController
    {
        private AllMoviesContext db = new AllMoviesContext();

        // GET: api/Movies
        public IQueryable<MovieDTO> GetMovies()
        {
            var movies = from m in db.Movies
                         select new MovieDTO()
                         {
                             Id = m.Id,
                             Name = m.Name,
                             Genre = m.Genre,
                             Budget = m.Budget,
                             Overview = m.Overview,
                             release_date = m.release_date,
                             Ratings = m.Ratings.Select(r => new RatingDTO()
                             {
                                 Id = r.Id,
                                 userId = r.userId,
                                 rating_score = r.rating_score
                             }
                             ).ToList()
                         };

            return movies;
        }


        // GET: api/Movies/5
        [ResponseType(typeof(MovieDTO))]
        public async Task<IHttpActionResult> GetMovies(int id)
        {
            Movie m = await db.Movies.FindAsync(id);
            if (m == null)
            {
                return NotFound();
            }
            MovieDTO movie = new MovieDTO
            {
                Id = m.Id,
                Name = m.Name,
                Genre = m.Genre,
                Budget = m.Budget,
                Overview = m.Overview,
                release_date = m.release_date,
                Ratings = m.Ratings.Select(r => new RatingDTO()
                {
                    Id = r.Id,
                    userId = r.userId,
                    rating_score = r.rating_score
                }
                ).ToList()
            };

            return Ok(movie);
        }

        // PUT: api/Movies/5
        [ResponseType(typeof(void))]
        public async Task<IHttpActionResult> PutMovies(int id, Movie movie)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != movie.Id)
            {
                return BadRequest();
            }

            db.Entry(movie).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!MovieExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Movies
        [ResponseType(typeof(Movie))]
        public async Task<IHttpActionResult> PostMovies(Movie movie)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Movies.Add(movie);
            await db.SaveChangesAsync();

            return CreatedAtRoute("DefaultApi", new { id = movie.Id }, movie);
        }

        // DELETE: api/Movies/5
        [ResponseType(typeof(Movie))]
        public async Task<IHttpActionResult> DeleteMovie(int id)
        {
            Movie movie = await db.Movies.FindAsync(id);
            if (movie == null)
            {
                return NotFound();
            }

            db.Movies.Remove(movie);
            await db.SaveChangesAsync();

            return Ok(movie);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool MovieExists(int id)
        {
            return db.Movies.Count(e => e.Id == id) > 0;
        }
    }
}
Daniel
  • 3
  • 1
  • Possible duplicate of [ASP.NET MVC Page Won't Load and says "The resource cannot be found"](https://stackoverflow.com/questions/893552/asp-net-mvc-page-wont-load-and-says-the-resource-cannot-be-found) – Linda Lawton - DaImTo Dec 13 '18 at 10:10
  • 1
    The error says "Requested URL: **/Movies**", but the code comment says "GET: **api/Movies**". Have you tried calling `/api/Movies` ? Then again: the method name is "GetMovies", have you tried calling `/Movies/GetMovies` or `/api/Movies/GetMovies` ? – Peter B Dec 13 '18 at 10:11
  • Thanks for the response Peter. Looking into it further, it seems my URL should be /api/Movies. Although I'm not sure how to do that at the moment. I know how to change the default url, but not for specific pages. Cheers. You've pointed me in the right direction. – Daniel Dec 13 '18 at 13:00

0 Answers0