简体   繁体   中英

Custom ASP.NET callback-based routing; dynamic ASPX page instantiation and rendering

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.

我参加了该项目的MVC,但是从那时起,我就有机会稍微剖析了ASP.NET请求管道,并根据需要实施了自定义路由解决方案。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM