嘗試使用EntityFramework添加具有讀/寫操作的MVC控制器。我的控制器名稱是UsersController。當我點擊添加,VS創建一個名爲用戶和子目錄像創建,編輯,索引等文件夾。使用EntityFramework的MVC控制器404錯誤
我的實體模型表名稱是用戶。
當我嘗試使用下面的鏈接與他們聯繫時,我得到「資源無法找到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 }
);
}
我userscontroller的樣子;
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);
}
}
}
謝謝!
也許你錯過了控制器之前的公共訪問修飾符...你能展示你的控制器是怎麼樣的嗎? – CoffeeCode
我添加了我的用戶控制器代碼來提問。謝謝! –