您能以编程方式定义 ASP.NET 配置吗?

发布于 2024-09-13 06:33:10 字数 413 浏览 9 评论 0原文

是否可以在代码中定义 ASP.NET 应用程序的大部分(如果不是全部)web.config?如果是这样,怎么办?您会使用 IHttpModule 吗?同样,您可以解析所述模块中的 IHttpHandler 来处理所有传入请求吗?

编辑1:最后一点是由对另一个问题的回答< /a>.

编辑2:我真正想做的是在代码中添加/删除模块和处理程序,而不是在 web.config 中。我可能至少需要在 web.config 中设置一个模块来允许这样做。然后我可以注册其他模块和处理程序吗?我只是在探索可能性。

Is it possible to define a large portion, if not the entire, web.config of an ASP.NET application in code? If so, how? Would you use an IHttpModule? In the same vein, can you resolve an IHttpHandler within said module to handle all incoming requests?

Edit 1: The last bit was instigated by this answer to another question.

Edit 2: What I really want to do is add/remove modules and handlers in code as opposed to the web.config. I probably need to at least set a module in the web.config that would allow this. Can I then register additional modules and handlers? I'm just exploring possibilities.

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

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

发布评论

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

评论(2

软的没边 2024-09-20 06:33:10

您可以在运行时更改它。这里概述了说明和可能的陷阱: http: //www.beansoftware.com/ASP.NET-Tutorials/Modify-Web.Config-Run-Time.aspx

我见过一些在安装或维护过程中修改配置的 Web 应用程序。 (DotNetNuke 在安装过程中执行此操作,AspDotNetStorefront 在配置向导中更改了多个设置。)

但请记住,每次更改 web.config 时,应用程序都需要重新编译,因此这可能会很麻烦。您最好将设置保存在数据库中并尽可能使用这些设置。更容易修改并且破坏性更小。

You can change it at run-time. Instructions and possible pitfalls are outlined here: http://www.beansoftware.com/ASP.NET-Tutorials/Modify-Web.Config-Run-Time.aspx

I've seen several web apps that modify the configuration during an Installation or Maintenance process. (DotNetNuke does it during install, and AspDotNetStorefront changes several settings as part of the configuration wizard.)

But remember that every time you change the web.config, the app needs to recompile, so it can be an annoyance. You'd be better off saving settings in a database and using those where you can. Easier to modify and less disruptive.

叫思念不要吵 2024-09-20 06:33:10

您可以使用 PreApplicationStartupMethod 在应用程序启动时在代码中注册 HttpHandler,而不是修改配置。示例代码(来自 Nikhil Kothari 的博客文章):

[assembly: PreApplicationStartMethod(typeof(UserTrackerModule), "Register")]

namespace DynamicWebApp.Sample {

    public sealed class UserTrackerModule : IHttpModule {

        #region Implementation of IHttpModule
        void IHttpModule.Dispose() {
        }

        void IHttpModule.Init(HttpApplication application) {
            application.PostAuthenticateRequest += delegate(object sender, EventArgs e) {
                IPrincipal user = application.Context.User;

                if (user.Identity.IsAuthenticated) {
                    DateTime activityDate = DateTime.UtcNow;

                    // TODO: Use user.Identity and activityDate to do
                    //       some interesting tracking
                }
            };
        }
        #endregion

        public static void Register() {
            DynamicHttpApplication.RegisterModule(delegate(HttpApplication app) {
                return new UserTrackerModule();
            });
        }
    }
}

另请参阅 Phil Haack 的帖子, 三个隐藏ASP.NET 4 中的可扩展性 Gem

Rather than modifying configuration, you can register HttpHandlers at application startup in code using the PreApplicationStartupMethod. Example code (from Nikhil Kothari's blog post):

[assembly: PreApplicationStartMethod(typeof(UserTrackerModule), "Register")]

namespace DynamicWebApp.Sample {

    public sealed class UserTrackerModule : IHttpModule {

        #region Implementation of IHttpModule
        void IHttpModule.Dispose() {
        }

        void IHttpModule.Init(HttpApplication application) {
            application.PostAuthenticateRequest += delegate(object sender, EventArgs e) {
                IPrincipal user = application.Context.User;

                if (user.Identity.IsAuthenticated) {
                    DateTime activityDate = DateTime.UtcNow;

                    // TODO: Use user.Identity and activityDate to do
                    //       some interesting tracking
                }
            };
        }
        #endregion

        public static void Register() {
            DynamicHttpApplication.RegisterModule(delegate(HttpApplication app) {
                return new UserTrackerModule();
            });
        }
    }
}

Also see Phil Haack's post, Three Hidden Extensibility Gems in ASP.NET 4.

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