MVC控制器使用EntityFramework 404错误

时间:2013-07-09 12:21:59

标签: asp.net-mvc entity-framework-4 http-status-code-404

尝试使用EntityFramework添加具有读/写操作的MVC控制器。我的控制器名称是UsersController。当我点击添加时,VS会创建一个名为Users和子目录的文件夹,如Create,Edit,Index等。

我的实体模型表名称是Users。

当我尝试使用下面的链接与他们联系时,我收到“无法找到资源404”错误。

http://localhost:64871/Users <br>
http://localhost:64871/Users/Index<br>
http://localhost:64871/Users/Create

我可以使用空的MVC控制器选项来创建我创建的链接。

http://localhost:64871/Home<br>
http://localhost:64871/Home/Test

有什么不对?我应该在RouteConfig文件中添加一些值吗?

我的RouteConfig.cs

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

    }

我的用户控件看起来像;

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

namespace Aplatform.Controllers
{
public class UsersController : Controller
{
    private dbEntities db = new dbEntities();

    //
    // GET: /Users/

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

    //
    // GET: /Users/Details/5

    public ActionResult Details(Guid id = null)
    {
        Users users = db.Users.Find(id);
        if (users == null)
        {
            return HttpNotFound();
        }
        return View(users);
    }

    //
    // GET: /Users/Create

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

    //
    // POST: /Users/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Users users)
    {
        if (ModelState.IsValid)
        {
            users.ID = Guid.NewGuid();
            db.Users.Add(users);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(users);
    }

    //
    // GET: /Users/Edit/5

    public ActionResult Edit(Guid id = null)
    {
        Users users = db.Users.Find(id);
        if (users == null)
        {
            return HttpNotFound();
        }
        return View(users);
    }

    //
    // POST: /Users/Edit/5

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

    //
    // GET: /Users/Delete/5

    public ActionResult Delete(Guid id = null)
    {
        Users users = db.Users.Find(id);
        if (users == null)
        {
            return HttpNotFound();
        }
        return View(users);
    }

    //
    // POST: /Users/Delete/5

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

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

}

谢谢!

2 个答案:

答案 0 :(得分:1)

我遇到类似的情况,默认路由不处理路径。我添加了后备路由来处理这些情况。试试这个:

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: "Users",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Users", action = "Index", id = UrlParameter.Optional }
    );
}

答案 1 :(得分:0)

过了一会儿,想出一些新的东西。如果控制器中有错误(语法错误等),则可能会出现404错误。如果您清除了错误,404将自动消失。