【问题标题】:The resource cannot be found MVC 4找不到资源 MVC 4
【发布时间】:2014-05-23 15:40:18
【问题描述】:

我正在开发 Visual Studio,MVC。

我生成了一个 ADO 模型,使用该模型创建了一个新控制器并更改了路由,但我的控制器似乎无法使用,即使我尝试从另一个控制器重定向。

我的控制器:

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);
    }
}
}

我的路线:

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 }
        );
    }
}
}

知道为什么 Visual Studio 说我找不到资源吗?

【问题讨论】:

  • 您是否针对每个操作创建了视图?
  • 不应该是public class leIndexController : Controller吗?
  • 是的,Visual Studio 生成了带有视图的控制器,因此它在视图中创建了一个名为“leIndex”的目录,其中包含 5 个文件:索引、创建、编辑、详细信息和删除。
  • simple-talk.com/dotnet/asp.net/… - 阅读ASP.NET MVC Controllers at a Glance。我仍然坚持要你重命名你的班级。

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

ASP.NET MVC 的控制器工厂,遵循一个命名约定,你的控制器应该遵循它——默认情况下。您可以通过创建自己的工厂来更改它,但现在,请更改

public class leIndex : Controller { ... }

public class leIndexController : Controller { ... }

【讨论】:

    猜你喜欢
    • 2023-03-15
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多