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/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With