2

I am working with a "legacy system" that is a large ASP.NET Web Forms site. A main extensibility point of this system is writing a dynamically loaded and rendered .ascx controls.

A web site project has ASP.NET MVC 3 dependencies referenced, and even has a ground for MVC usage by introducing the following route into the ASP.NET 4 routing system:

    routes.MapRoute(
      "Default", // Route name
      "{controller}.mvc/{action}/{pid}", // URL with parameters
      new { controller = "Home", action = "Index", pid = "" } // Parameter defaults

According to this route, everything, that is to be called by the url pattern of {controller}.mvc will be rendered according to MVC paradigm.

In my ascx control (I can't avoid using ASCX, I have to live with it), I am making a call to my controller from code:

<% 
    var controller = new NamingsController(DependencyResolver.Current.GetService<InDocContext>());
    var htmlToRender = controller.RenderExistingNamings(); 
%>

   <%=htmlToRender%>

This way of calling a controller is wrong - I am creating it manually, whereas in the "normal way" it is getting created by the controller factory. In my case, the request context, and therefore a controller context are not populated and my controller is no better than a standard logic class.

What is the correct way of invoking a controller's action in my case, so that it executes as if a user made a call to it via the browser?

2 Answers 2

1

Try the following:

<%
    var routeData = new RouteData();
    routeData.Values["controller"] = "Namings";
    routeData.Values["action"] = "RenderExistingNamings";        
    IController controller = new NamingsController(DependencyResolver.Current.GetService<InDocContext>());
    var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
    controller.Execute(rc);        
%>

Notice that there's no <%=htmlToRender%>. The output will be immediately written to the response.

You should be careful with the Content-Type response header that will be now the content type set by your controller action and not the content type set by your WebForm. If both are text/html it should be an issue.

Sign up to request clarification or add additional context in comments.

1 Comment

Darin,did you lookup the MVC source to figure that out? =) It worked! Although I had to change the second line to ` routeData.Values["controller"] = "Namings";` since using it with NamingsController were affecting the way system searched for Views. Thank you very much.
0

Have you tried using Html.RenderAction() instead?

http://msdn.microsoft.com/en-us/library/system.web.mvc.html.childactionextensions.renderaction.aspx

2 Comments

Unfortunately, doesn't work. I get error CS0234: The type or namespace name 'RenderAction' does not exist in the namespace 'System.Web.Mvc.Html' (are you missing an assembly reference?) even though I am referencing System.Web.Mvc.Html. Probably because these HTML helpers are build to be called from within an MVC view, that is being rendered as a part of a controller execution cycle already.
But isn't your ASCX within the execution cycle of a controller? I'm not too familiar with ASCXs since I use MVC with Razor, but as far as I know HtmlHelper should be accesible to you as well. Does your control inherit from ViewUserControl<>?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.