自定义基于 ASP.NET 回调的路由;动态 ASPX 页面实例化和渲染

发布于 2025-01-05 10:07:42 字数 1774 浏览 2 评论 0原文

我正在开发一个小型学校项目,一个 ASP.NET C# 网站;我们正在使用一个 Web 应用程序,使用 Global.asax 文件来集中请求逻辑。

不管怎样,我和我的同事负责我们小组的编码,我们都是经验丰富的 PHP 开发人员。我们都非常喜欢使用 PHP 框架 Laravel 使用的架构风格,使用路由(与 < /em>)作为“控制器”,并且(尽管它是一个方钉,圆孔问题)正在尝试为该项目复制该功能。

这不是一件容易的事;我已经实现了 IRouteHandler 接口作为 CallbackRouteHandler 尝试开始复制此功能:

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);
    }
}

不幸的是,这只是我所了解的。我正在阅读 ASP.NET 页面生命周期概述,尝试以便更好地了解整个过程。

我们所坚持的是在给定的路由回调范围内以编程方式加载 ASPX 文件(而是实例化为 Page 对象)。我们希望在回调的范围内有一种相当简单的方法来完成,例如:

// 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();

我无法理解两者:1) 如何做到这一点? 2) 何时(相对于上述页面生命周期)执行此操作。

如何(如果有的话)能够实现这种功能?我发现这个被 ASP.NET 隐藏的过程看起来非常复杂,但肯定其他人以前已经走过这条路。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心 2025-01-12 10:07:43

我在这个项目中使用了 MVC,但此后我有机会稍微剖析 ASP.NET 请求管道,并根据需要实现了自定义路由解决方案。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文