2013-02-19 20 views
0

이 컨트롤러의 생성자가 "} expected"오류를 반환합니다. Visual Studio에서이를 생성자로 인식하지 못합니다. 다른 오류에는 "메서드에 반환 형식이 있어야합니다"가 포함됩니다.Asp.net mvc 컨트롤러 생성자가 오류를 반환했습니다.

이 코드에 어떤 문제가 있습니까?

namespace blahblah.Controllers 
{ 

    public class HomeController : Controller 
    { 
     private IUserRepository userRepository; 

     public HomeController() 
     {  //} expected here 
      public Model1Container db = new Model1Container(); 
      this.userRepository = new UserRepository(db); 
     } 

     public ActionResult Index() 
     { 
      ViewBag.Message = "Welcome to ASP.NET MVC!"; 

      return View(); 
     } 

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

답변

3

귀하의 생성자는 다음과 같아야합니다

public HomeController() 
{ 
    Model1Container db = new Model1Container(); 
    this.userRepository = new UserRepository(db); 
} 

제거 단어 public를 로컬 변수 db 근처. 액세스 한정자는 로컬 변수로 액세스 할 수 없습니다. 범위는 선언 된 메소드 본문으로 제한되기 때문입니다. C# 언어 사양 4.0

:

로컬 변수 선언은 유형 이름 변수 이름 아마도 초기 값을 지정한다.