Fortune
Fortune

Reputation: 533

The resource cannot be found MVC 4

I'm working on Visual Studio, MVC.

I generated an ADO model, created a new controller with this model and changed the route but my controller seem inusable, even if i try to redirect from another controller.

My controller :

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Garnak.Models;

namespace Garnak.Views
{
public class leIndex : Controller
{
    private garnakEntities db = new garnakEntities();

    //
    // GET: /leIndex/

    public ActionResult Index()
    {
        return View(db.compteSet.ToList());
    }

    //
    // GET: /leIndex/Details/5

    public ActionResult Details(int id = 0)
    {
        compteSet compteset = db.compteSet.Find(id);
        if (compteset == null)
        {
            return HttpNotFound();
        }
        return View(compteset);
    }

    //
    // GET: /leIndex/Create

    public ActionResult Create()
    {
        return View();
    }

    //
    // POST: /leIndex/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(compteSet compteset)
    {
        if (ModelState.IsValid)
        {
            db.compteSet.Add(compteset);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(compteset);
    }

    //
    // GET: /leIndex/Edit/5

    public ActionResult Edit(int id = 0)
    {
        compteSet compteset = db.compteSet.Find(id);
        if (compteset == null)
        {
            return HttpNotFound();
        }
        return View(compteset);
    }

    //
    // POST: /leIndex/Edit/5

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(compteSet compteset)
    {
        if (ModelState.IsValid)
        {
            db.Entry(compteset).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(compteset);
    }

    //
    // GET: /leIndex/Delete/5

    public ActionResult Delete(int id = 0)
    {
        compteSet compteset = db.compteSet.Find(id);
        if (compteset == null)
        {
            return HttpNotFound();
        }
        return View(compteset);
    }

    //
    // POST: /leIndex/Delete/5

    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        compteSet compteset = db.compteSet.Find(id);
        db.compteSet.Remove(compteset);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

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

My route :

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

namespace Garnak
{
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 = "leIndex", action = "Index", id =        UrlParameter.Optional }
        );
    }
}
}

Any idea why Visual studio say me that the ressource cannot be found ?

Upvotes: 0

Views: 2494

Answers (1)

amiry jd
amiry jd

Reputation: 27605

ASP.NET MVC's controller factory, follows a naming convention that your controllers should follow it -by default. You can change it by creating your own factory, but for now, change

public class leIndex : Controller { ... }

to

public class leIndexController : Controller { ... }

Upvotes: 2

Related Questions