1

I'm following this tutorial on microsoft.com. It was all working fine till I reached to this page. I've copied the exact code for both Model and connection string but I'm getting the error of Resource not found. enter image description here

As the version of VS used in the tutorial is different to mine(mine is VS 2013). I've tried every solution I could Google but no use. I'm trying to solve this for more than 6 hours.

Editing the post as asked in the comments:

Code for Controller:

namespace MvcMovie.Controllers
{
    public class MoviesController : Controller
    {
        private MovieDBContext db = new MovieDBContext();

        // GET: /Movies/
        public ActionResult Index()
        {
            return View(db.Movies.ToList());
        }

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

Views/Movies/Index.cshtml:

@model IEnumerable<MvcMovie.Models.Movie>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ReleaseDate)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Genre)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Price)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ReleaseDate)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Genre)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Price)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

</table>

RouteConfig.cs:

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

namespace MvcMovie
{
    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 = "Home", action = "Index", id = UrlParameter.Optional }
            );
            routes.MapRoute(
           name: "Hello",
           url: "{controller}/{action}/{name}/{id}"
       );
        }
    }
}

I'm trying to open: http://localhost:51033/Views/Movies/Index.cshtml Also when I checked the AppData Folder, it's empty. So the code first DB is not created.

17
  • 4
    The error message is a 404. That has nothing to do with a connection string. It means the server couldn't find something the browser requested. Based on the URL in the error rmessage, it appears you're trying to request a view. But you shouldn't do that. You should write a request that routes to a controller action, the action will then be responsible for returning the view. You should go through some tutorials on routing again until you understand it. Commented Dec 16, 2018 at 15:12
  • 1
    Why you are requesting /*.cshtml, that is not the way how ASP.NET MVC works. You should put /{controller}/{action} only, if that is the mapping you defined in your app start-up. Commented Dec 16, 2018 at 17:26
  • 1
    Ok, as @wannadream said, try removing .cshtml from your URL. So just put localhost:<yourport>/<youralias>/Movies and check if it opens the page you want. In MVC, the control goes to controller first and then it calls the view. Try also to put a break point on your Index method and check if it is hit when you go to the URL. Commented Dec 16, 2018 at 17:50
  • 1
    @Waz I said it all in the first comment. Your URL doesn't match your route. You need to learn about routing. It has nothing to do with your database. Commented Dec 16, 2018 at 18:01
  • 1
    Possible duplicate of Running a copy of a .cshtml file gives a 404 Commented Dec 16, 2018 at 18:43

2 Answers 2

2

You are requesting wrong Url in wrong approach. http://localhost:51033/Views/Movies/Index.cshtml cannot be an Url in ASP.NET MVC. Please try with the following Url:

 http://localhost:51033/Movies/Index

Your problem should fix!

Sign up to request clarification or add additional context in comments.

Comments

0

After trying for so many solutions including the above answer I've came to the ultimate solution of my problem. I basically had two problems:

  1. Invalid URL for MVC (although I did try http://localhost:51033/Movies for multiple times, but still the URL generated by the solution, which I was using was wrong).
  2. My real problem was my connection string (which I was expecting). Apparently my LocalDB is not working for some reasons. So I used SQL Server installed on my system instead and it worked like a charm.

So, if anyone else faces this issue then they can try a connection string like the one below:

<add name="MovieDBContext"
    providerName="System.Data.SqlClient"
    connectionString="Data Source=.;Initial Catalog=MvcMovie;Integrated Security=True" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.