2012-01-17 28 views
8

Khi tôi cố gắng xây dựng dự án trong asp.net mvc3. 27 lỗi xuất hiện, nói rằng các lớp MVC liên quan không tồn tại:Lỗi Không thể tìm thấy loại hoặc không gian tên 'Bộ điều khiển' (bạn đang thiếu một chỉ thị sử dụng hoặc tham chiếu assembly?)

Dưới đây là một ví dụ về một:

Error 1 The type or namespace name 'Controller' could not be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\hhhhhhhh\my documents\visual studio 2010\Projects\MvcApplication1\MvcApplication1\Controllers\HomeController.cs 10 35 MvcApplication1 

CẬP NHẬT

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

namespace MvcApplication2.Controllers 
{ 
    public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      ViewBag.Message = "Welcome to ASP.NET MVC!"; 

      return View(); 
     } 

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

Lỗi:

Error 1 The type or namespace name 'Controller' could not be found (are you missing a using directive or an assembly reference?) c:\documents and settings\hhhhhhhh\my documents\visual studio 2010\Projects\MvcApplication2\MvcApplication2\Controllers\HomeController.cs 9 35 MvcApplication2 

Error 2 The type or namespace name 'ActionResult' could not be found (are you missing a using directive or an assembly reference?) c:\documents and settings\hhhhhhhh\my documents\visual studio 2010\Projects\MvcApplication2\MvcApplication2\Controllers\HomeController.cs 11 16 MvcApplication2 


Error 3 The name 'ViewBag' does not exist in the current context c:\documents and settings\hhhhhhhh\my documents\visual studio 2010\Projects\MvcApplication2\MvcApplication2\Controllers\HomeController.cs 13 13 MvcApplication2 


Error 4 The name 'View' does not exist in the current context c:\documents and settings\hhhhhhhh\my documents\visual studio 2010\Projects\MvcApplication2\MvcApplication2\Controllers\HomeController.cs 15 20 MvcApplication2 


Error 5 The type or namespace name 'ActionResult' could not be found (are you missing a using directive or an assembly reference?) c:\documents and settings\hhhhhhhh\my documents\visual studio 2010\Projects\MvcApplication2\MvcApplication2\Controllers\HomeController.cs 18 16 MvcApplication2 


Error 6 The name 'View' does not exist in the current context c:\documents and settings\hhhhhhhh\my documents\visual studio 2010\Projects\MvcApplication2\MvcApplication2\Controllers\HomeController.cs 20 20 MvcApplication2 
+0

hãy đảm bảo rằng bạn đã bao gồm System.Web.MVC và System.Web.Mvc.Controller hoặc không vô tình xóa khoản sử dụng cho không gian tên MVC – Devjosh

+0

Tôi đã làm .. không nhận dạng được bộ điều khiển –

+0

trong trường hợp đó, vui lòng xem liên kết này nếu nó giúp http://stackoverflow.com/questions/6238295/the-type-or-namespace-name-servicecontroller-could-not-be-found @dmitry – Devjosh

Trả lời

4

Di using System.Web.Mvc.Controller; . Mệnh đề sử dụng được sử dụng để xác định không gian tên, không lớp:

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

namespace Controllers 
{ 
    public class HomeController : Controller 
    { 
     public ActionResult Index() 
     { 
      return View(); 
     } 

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

Ngoài ra hãy chắc chắn rằng bạn có System.Web.Mvc lắp ráp tham chiếu trong dự án của bạn.

Một điều cần lưu ý khác là không gian tên của bạn. Tôi thấy rằng bạn đang sử dụng namespace Controllers thay vì namespace AppName.Controllers. Đảm bảo rằng bạn không có namespace Controller (không có s) được xác định ở đâu đó trong dự án của bạn có thể xung đột với lớp Controller mà bạn đang cố gắng sử dụng tại đây.

+0

Để cho bạn biết sự thật, tôi không thể tìm thấy tham chiếu System.web.mvc khi tôi cố gắng thêm tham chiếu –

+1

@ DmitryMakovetskiyd, bạn đã cài đặt ASP.NET MVC 3 chưa? Ngoài ra hãy chắc chắn rằng bạn đang nhắm mục tiêu đầy đủ .NET 4.0 và không phải là Hồ sơ khách hàng .NET 4.0. –

17

Bạn sẽ cần thêm System.Web.Mvc vào tài liệu tham khảo dự án của mình.

  1. Nhấp chuột phải vào tài liệu tham khảo
  2. Bấm "Add Reference ..."
  3. Assemblies > Khung
  4. Assemblies tìm kiếm (Ctrl +E) cho System.Web.Mvc (bạn sẽ có thể có nhiều hơn: phiên bản 2/3/4)
+0

Điều này làm việc cho tôi sau khi tôi đã xóa tham chiếu cũ của System.Web.Mvc và xây dựng lại giải pháp và thêm lại nó thông qua Quy trình đề cập ở trên. – Jamil

7

Hôm nay tôi có cùng loại lỗi. Tôi đã cài đặt MVC phiên bản 3 và dự án đã có lỗi, đã chạy mà không có lỗi trước đó. Những gì tôi đã làm hôm nay là, tôi đã tự động cập nhật cửa sổ. Trong bản cập nhật MVC phiên bản 3 đã được tải xuống và cài đặt tự động. Vì vậy, các tài liệu tham khảo MVC phiên bản 3, đã được thêm vào trước trong dự án của tôi, không còn hợp lệ sau khi cập nhật các cửa sổ được cài đặt. Vì vậy, tôi đã xóa tham chiếu đó và thêm cùng một tham chiếu. Điều đó đã sửa lỗi của tôi. :)

-1

Bạn sẽ có được điều này trong Add Reference-> Extentions -> System.Web.Mvc

1

Có vẻ như bạn có dự án thử nghiệm trong dung dịch. Có hai cách để giải quyết các lỗi này

1) Loại trừ dự án thử nghiệm đơn vị khỏi giải pháp.

2) Bạn chỉ cần thêm tham chiếu dự án MVC vào dự án thử nghiệm. Đi tới dự án Thử nghiệm Đơn vị> Nhấp chuột phải vào Tham khảo> nhấp vào Thêm tham chiếu> Nhấp vào tab Dự án> Thêm tham chiếu dự án

Tại đây bạn đi !!!

Các vấn đề liên quan