简体   繁体   English

可以创建自定义ASP.NET(非MVC)指令吗?

[英]Create a custom ASP.NET (not MVC) directive possible?

I'm working on a somewhat advanced custom framework for some websites I'm developing at work, and I'm implementing a small plugin system for it. 我正在为我正在开发的一些网站开发一个有点高级的自定义框架,我正在为它实现一个小插件系统。 I want to register the plugins somehow in the ASPX file so I can then hook into the different page events (Load, PreRender, etc.) after calling them in sequence in my Page_Init function, for example. 我想以某种方式在ASPX文件中注册插件,以便我可以在我的Page_Init函数中按顺序调用它们之后挂钩到不同的页面事件(Load,PreRender等)。 I'll jump straight to the code so you see what I want to do: 我会直接跳到代码中,看看我想做什么:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Plugin TypeName="MyFramework.Plugin.Core" Arg1="arg1" Arg2="arg2" %>
<%@ Plugin TypeName="MyFramework.Plugin.MainMenu" Arg1="arg1" Arg2="arg2" %>
<%@ Plugin TypeName="MyFramework.Plugin.IE6Hacks" Arg1="arg1" Arg2="arg2" %>
<%@ Plugin TypeName="MyFramework.Plugin.Footer2015" Arg1="arg1" Arg2="arg2" %>
<%@ Plugin TypeName="MyFramework.Plugin.SortableGridViews" Arg1="arg1" Arg2="arg2" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>blah</title>
</head>
<body>      
    <form id="form1" runat="server"> Blah... </form>
</body>
</html>

See those <% Plugin %> directives? 看那些<% Plugin %>指令? they don't exist in ASP.NET but I want to create them. 它们在ASP.NET中不存在但我想创建它们。 I can't find anything on google regarding this stuff. 关于这个东西我在谷歌上找不到任何东西。 Is it possible to create a custom one? 是否可以创建自定义的?

My idea is that once they're "registered" with those directives, I can loop through all of them (either stored in an array everytime the directive is hit, or in the Page_Init event) and act accordingly. 我的想法是,一旦他们“注册”了这些指令,我就可以循环遍历所有指令(每次指令被命中时存储在数组中,或者在Page_Init事件中)并相应地采取行动。

Another idea I had was to use code blocks instead: 我的另一个想法是使用代码块:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<% RegisterPlugin("MyFramework.Plugin.Core"); %>
<% RegisterPlugin("MyFramework.Plugin.MainMenu"); %>
<% RegisterPlugin("MyFramework.Plugin.IE6Hacks"); %>
<% RegisterPlugin("MyFramework.Plugin.Footer2015"); %>
<% RegisterPlugin("MyFramework.Plugin.SortableGridViews"); %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title>blah</title>
</head>
<body>      
    <form id="form1" runat="server"> Blah... </form>
</body>
</html>

BUT the functions get called after all the page events instead of running before them (or at least before Page_Load) leaving me with no way of detecting if I have plugins registered in order to act accordingly in my base page's Page_Load events and such. 但是所有页面事件之后调用函数而不是在它们之前运行(或至少在Page_Load之前)让我无法检测我是否已经注册了插件以便在我的基页的Page_Load事件等中相应地执行操作。

Any ideas are really appreciated :). 任何想法都非常感谢:)。 Thanks in advance! 提前致谢!

The ASP.NET page parser doesn't support custom directives (although it does support custom attributes on the Page or MasterPage directive). ASP.NET页面解析器不支持自定义指令(尽管它支持Page或MasterPage指令的自定义属性)。 But you still can do what you want using server controls. 但你仍然可以使用服务器控件做你想做的事。

Let's suppose you have a base plugin class, like this: 假设您有一个基本插件类,如下所示:

// we derive from control, so all plugins are automatically
// added to the Page.Controls collection
public class MyPluginBase : Control
{
}

And for example, two plugin classes like this: 例如,两个插件类如下:

public class MyPlugin : MyPluginBase
{
    public string MyFirstProperty { get; set; }
    public int MySecondProperty { get; set; }
}

public class MyOtherPlugin : MyPluginBase
{
    public string MyFirstProperty { get; set; }
    public int MySecondProperty { get; set; }
}

Then you can write an ASPX webform (or an ASCX) like this: 然后你可以像这样写一个ASPX webform(或ASCX):

<%@ Page ... standard attributes ... %>

<plugins:MyPlugin runat="server" MyFirstProperty="blah" MySecondProperty="1"></plugins:MyPlugin>
<plugins:MyOtherPlugin runat="server" MyFirstProperty="blahblah" MySecondProperty="2"></plugins:MyOtherPlugin>

etc...

Note for this to work, you'll have to declare the plugins prefix somewhere, for example in the web.config, like this: 注意这个工作,你必须在某处声明插件前缀,例如在web.config中,如下所示:

<configuration>
  <system.web>
    <pages>
      <controls>
        <!-- all assemblies & namespaces that contain plugins classes -->
        <add assembly="WebApplication1" namespace="WebApplication1.Code" tagPrefix="plugins" />
        <add assembly="MyOtherPlugins" namespace="MyOtherPlugins.Plugins" tagPrefix="plugins" />
      </controls>
    </pages>
  </system.web>
</configuration>

And in the code behind class, you'll have automatic access to the plugins, in all events including Init, with a code like this: 在类后面的代码中,您可以在包括Init在内的所有事件中自动访问插件,代码如下:

public partial class WebForm1 : Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        // get all plugins on the page
        var plugins = Controls.OfType<MyPluginBase>();
    }
}

Use a dependency injection container like Autofac that has support for WebForms . 使用像Autofac这样的依赖注入容器,它支持WebForms

You're probably going to be interested in using the Enumeration or Metadata Integerrogation relationship types to accomplish your plugin registration/resolution. 您可能会对使用EnumerationMetadata Integerrogation关系类型感兴趣来完成插件注册/解析。

You might also find some traction using MEF with WebForms. 您可能还会发现使用MEF和WebForms的一些牵引力。 WebForms and MEF Sample WebForms和MEF示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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