0

I'm working on a small school project, an ASP.NET C# website; we're working with a Web Application, using a Global.asax file to centralize request logic.

Anyway, my colleague and I are responsible for the coding in our group, and we both come as reasonably experienced PHP developers. We both rather enjoy working with the architectural style used by the PHP framework Laravel, using routes (callbacks associated with) as the "controllers", and (despite it being a square peg, round hole issue) are trying to replicate that functionality for this project.

This is no easy task; I've implemented the IRouteHandler interface as a CallbackRouteHandler in an attempt to start replicating this functionality:

public class CallbackRouteHandler : IRouteHandler
{
    public Func<RequestContext, IHttpHandler> Callback { get; protected set; }

    public CallbackRouteHandler(Func<RequestContext, IHttpHandler> callback)
    {
        this.Callback = callback;
    }

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return this.Callback(requestContext);
    }
}

Unfortunately this is about as far as I've gotten. I'm reading through the ASP.NET Page Life Cycle Overview, attempting to understand better the entire process.

What we're stuck on is programmatically loading ASPX files (rather, instantiating as Page objects) in the scope of a given route callback. We were hoping there would be a reasonably easy way to accomplish, within the scope of the callback, something like:

// instantiate the target ASPX page object
OurSite.SomeNamespace.SomePage page = new OurSite.SomeNamespace.SomePage();

// manipulate the page object, setting public properties, etc.
page.SomeControl.Text = "Foobar!";

// eventually "render" the file to somehow; at this point, the
// page and it's associated code-behind events take control
page.Render();

I'm having trouble understanding both: 1) How to do this? 2) When (relative to the aforementioned page life-cycle) to do this.

How (if at all) can one accomplish this sort of functionality? I'm seeing that this process, hidden away by ASP.NET, is seemingly very complicated, but surely others have tread down this path before.

Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
  • 1
    Switch to MVC and this all becomes infinitely easier. – M.Babcock Feb 15 '12 at 05:36
  • Have you looked into ASP.NET MVC for this functionality or is developing this part of your project? – Jared Shaver Feb 15 '12 at 05:38
  • +1 @M.Babcock -- Surely, however this is the route (*forgive me*) we've taken for the time being. I'm investigating MVC, but understanding the request process will surely help me anyways. – Dan Lugg Feb 15 '12 at 05:40
  • @JaredShaver It's not a requirement, no. However, I'm curious about this from an academic standpoint. I'm familiar with the MVC stack in other platforms, so I doubt it would be particularly difficult to use, however my team-mate and I have both worked considerably with the Laravel framework in PHP, and were hoping to replicate that functionality, as a convenience in practice, and a learning experience in .NET. – Dan Lugg Feb 15 '12 at 05:43
  • 1
    MVC is open source, you can view it here: http://aspnet.codeplex.com/SourceControl/BrowseLatest Since it sounds like you want to re-implement it, you may as well look at the source for the implementation. – Jared Shaver Feb 15 '12 at 05:43
  • +1 @JaredShaver -- Thank you, that's extremely helpful. I wouldn't say we wish to "re-implement" the MVC framework; we just want to extend the simple routing support of web applications to act more like the PHP framework I won't shut up about :) I thought that dynamically instantiating, and eventually rendering, ASPX files would be trivial (*especially before I starting poking my nose around in this*) but clearly it's not. – Dan Lugg Feb 15 '12 at 05:52
  • MVC has a WebForms (aspx) view engine that you could look at. If you're really serious about it, you may want to pick up Dino Esposito's MVC 2 book which goes into all the details on how the WebForms view engine renders from start to finish. – Jared Shaver Feb 15 '12 at 05:59
  • Screw it. I'm going MVC. (*note; still curious on any related reading material, suggestions, etc., so I'm leaving this question here*) – Dan Lugg Feb 15 '12 at 06:32

1 Answers1

0

I went with MVC for this project, however I've since had the opportunity to dissect the ASP.NET request pipeline a bit, and have implemented custom routing solutions as warranted.

Dan Lugg
  • 20,192
  • 19
  • 110
  • 174