Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run asp.net mvc application from linqpad or console application

How can i start and get ActionResult from some actions of my mvc application use only Linqpad or console application?

I know i can create MvcApplication class instanse:

var mvcApplication = new Web.MvcApplication();

then create controller:

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

even run controller's action

homeController.Index()

but it returns nothing. What is lifecycle of mvc application? Which methods i shoud call to emulate web request from user?

EDIT


Here some good posts about ASP.NET MVC lifecycle, but unfortunally i cannot resolve my problem yet

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/

like image 911
Neir0 Avatar asked Dec 07 '25 03:12

Neir0


1 Answers

I know this is an old question, and might be answered elsewhere, but in a project I am working on we can debug Controller Actions with Linqpad and get returned values.

Briefly, you need to tell Linqpad to return something:

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

You may also need to mock your context and attach visual studio as the debugger.

Full code snippet:

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]";} }
}

You should get prompted to select a debugger, select the instance of Visual Studio with your app built.

We have a specific set up for MVC-CRM so this may not work for everyone, but hopefully this will help someone else out.

like image 87
red_dorian Avatar answered Dec 08 '25 17:12

red_dorian