2013-07-09 28 views
1

嘗試使用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); 
    } 
} 

}

謝謝!

+0

也許你錯過了控制器之前的公共訪問修飾符...你能展示你的控制器是怎麼樣的嗎? – CoffeeCode

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

嗨djpark。我將你的示例代碼添加到我的RouteConfig.cs中,但沒有運氣,結果相同。你有其他想法嗎?謝謝。 –

+0

再次嗨。生成項目後,您的解決方案就可以運行再次感謝djpark。! –

0

過了一段時間後,找出新的東西。如果你的控制器有錯誤(語法錯誤..等),你可能會得到404錯誤。如果清除該錯誤,404將自動消失。