2013-03-18 5 views
3

내 mvc 응용 프로그램의 일부 동작에서 ActionResult를 시작하고 가져올 수있는 방법은 Linqpad 또는 콘솔 응용 프로그램 만 사용합니까?Linqpad 또는 콘솔 응용 프로그램에서 asp.net mvc 응용 프로그램을 실행하십시오.

var homeController = new Web.Controllers.HomeController(); 

도 실행 컨트롤러의 동작

homeController.Index() 

하지만 아무 것도 반환하지 :

var mvcApplication = new Web.MvcApplication(); 

다음 컨트롤러를 만들 :

나는 내가 MvcApplication 클래스 instanse를 ​​만들 수 있습니다 알고 있습니다. mvc 응용 프로그램의 수명주기 란 무엇입니까? 어떤 메소드를 호출하여 사용자로부터 웹 요청을 에뮬레이션합니까?

편집


ASP.NET MVC 라이프 사이클에 대한 몇 가지 좋은 게시물 여기

하지만,하지만 회로 내가 해결할 수없는 내 문제는 아직

http://stephenwalther.com/archive/2008/03/18/asp-net-mvc-in-depth-the-life-of-an-asp-net-mvc-request.aspx

http://blog.stevensanderson.com/2007/11/20/aspnet-mvc-pipeline-lifecycle/

+0

그런 위업의 목표가 무엇인지 물어봐도 될까요? –

+0

@Kenneth K 주요 목적은 테스트 및 버그 수정을보다 쉽게하는 것입니다. Ofcourse 단위 테스트를 사용할 수 있지만 그들은 Linqpad에서 덤프 같은 강력한 출력 방법을 havent. – Neir0

+0

@ 케네스 케이 또 다른 이유는 asp.net mvc 응용 프로그램이 실제로 어떻게 작동하는지 이해하는 것입니다. 당신이 당신의 자신의 손으로 그것을 시도 할 때 그것은 다만 더 나은 어떤 기사든지 다만 읽는다. – Neir0

답변

2

나는 이것이 알고 이전 질문, 그리고 다른 곳에서 답변을 얻을 수도 있지만, 우리가 작업하고있는 프로젝트에서 Linqpad로 Controller Actions를 디버깅 할 수 있습니다. 반환 된 값을 가져옵니다.

var result = homeController.Index(); 
result.Dump(); 

당신은 또한 당신의 상황을 조롱하고 디버거로 비주얼 스튜디오를 연결해야 할 수도 있습니다

간단히 말해서, 당신은 뭔가를 반환하는 Linqpad 말할 필요가있다.

전체 코드는 :

void Main() 
{  
    using(var svc = new CrmOrganizationServiceContext(new CrmConnection("Xrm"))) 
    { 
     DummyIdentity User = new DummyIdentity(); 

     using (var context = new XrmDataContext(svc)) 
     { 
      // Attach the Visual Studio debugger 
      System.Diagnostics.Debugger.Launch(); 
      // Instantiate the Controller to be tested 
      var controller = new HomeController(svc); 
      // Instantiate the Context, this is needed for IPrincipal User 
      var controllerContext = new ControllerContext(); 

      controllerContext.HttpContext = new DummyHttpContext(); 
      controller.ControllerContext = controllerContext; 

      // Test the action 
      var result = controller.Index(); 
      result.Dump(); 
     } 
    }    
} 

// Below is the Mocking classes used to sub in Context, User, etc. 
public class DummyHttpContext:HttpContextBase { 
    public override IPrincipal User {get {return new DummyPrincipal();}} 
} 

public class DummyPrincipal : IPrincipal 
{ 
    public bool IsInRole(string role) {return role == "User";} 
    public IIdentity Identity {get {return new DummyIdentity();}} 
} 

public class DummyIdentity : IIdentity 
{ 
    public string AuthenticationType { get {return "?";} } 
    public bool IsAuthenticated { get {return true;}} 
    public string Name { get {return "[email protected]";} } 
} 

당신은 디버거를 선택 내장 앱을 비주얼 스튜디오의 인스턴스를 선택하라는 메시지가 받아야합니다.

MVC-CRM에 대한 특정 설정이 있으므로 모든 사용자에게 적용되지는 않습니다.하지만 다른 사용자에게 도움이되기를 바랍니다.

관련 문제