c# - 在 ASP.NET 应用程序中以编程方式设置 HTTP 处理程序

标签 c# .net asp.net

我需要从控制台应用程序动态实例化 Web 应用程序。根据这个定义,我的意思是我的控制台应用程序包含一个未绑定(bind)到 IIS/XSP 的 Web 应用程序。

Currently ,我将 Web 应用程序创建到一个临时目录中,并将一些伪造 文件复制到其中。这些是映射到我自己的 HttpApplication 实现以在 Web 应用程序中使用的特殊 Global.asax(我需要在应用程序启动时进行一些初始化),然后我伪造映射到我自己的骨架类的特殊 .asmx 文件 动态插件

            foreach (IPlugin plugin in _target.Plugins)
            {
                WsdlSkeletonDefinition[] defs = plugin.GetWsdlSkeletons();
                if (defs != null)
                    foreach (WsdlSkeletonDefinition def in defs)
                    {
                        if (def.SkeletonType == null)
                            throw new LogbusException(string.Format("Plugin {0} declares empty skeleton type",
                                                                    plugin.Name));
                        if (def.SkeletonType.IsAssignableFrom(typeof(System.Web.Services.WebService)))
                            throw new LogbusException(
                                string.Format("Plugin {0} does not declare a valid WSDL skeleton type", plugin.Name));

                        string fname = def.UrlFileName;
                        if (fname.EndsWith(".asmx", false, CultureInfo.InvariantCulture))
                            fname = fname.Substring(0, fname.Length - 5);

                        if (!Regex.IsMatch(fname, @"^[a-zA-Z0-9_\.\-%]+$", RegexOptions.CultureInvariant))
                            throw new LogbusException(string.Format(
                                "Plugin {0} declares invalid WSDL endpoint: {1}",
                                plugin.Name, def.UrlFileName));

                        string wsDeclaration = string.Format(ASMX_TEMPLATE, def.SkeletonType.AssemblyQualifiedName);

                        using (
                            StreamWriter sw = new StreamWriter(File.Create(Path.Combine(_physicalPath, fname + ".asmx")),
                                                               Encoding.Default))
                            sw.Write(wsDeclaration);

                        //Copy skeleton asembly if needed
                        CopyAssemblyTo(def.SkeletonType.Assembly, bindir);
                        foreach (AssemblyName dependency in def.SkeletonType.Assembly.GetReferencedAssemblies())
                        {
                            try
                            {
                                CopyAssemblyTo(Assembly.Load(dependency), bindir);
                            }
                            //Possible broken dependency
                            catch { }
                        }
                    }
            }

我的方法可行,但我对此并不满意,因为我必须将大量垃圾写入文件系统,即使我最终将其全部删除也是如此。

我知道我可以通过 Web.config 控制 HTTP 处理程序,但我不想为此伪造 Web.config。我想创建一个映射,例如我可以从 Web 服务的 URL 中删除 .asmx 扩​​展名并仍然获得它们。

例如,默认脚本之一是“LogbusManagement.asmx”,它必须硬编码到客户端 API 中,而 .asmx 会阻止可移植到其他平台(例如 PHP)。我想让“LogbusManagement.asmx”等同于“LogbusManagement”和任何扩展。为此,我可能会使用 HttpHandlerFactory。

我的直接问题是,

点赞here其他人:有没有办法以编程方式(可能来自 Global.asax)为 Web 应用程序设置 IHttpHandlers 或 IHttpHandlerFactories?

谢谢

最佳答案

这个问题已经在 stackoverflow 中得到了回答:

Any way to add HttpHandler programatically in .NET?

关于c# - 在 ASP.NET 应用程序中以编程方式设置 HTTP 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4101858/

相关文章:

c# - 按下按钮并成功重定向到另一个页面后,如何禁用导航栏链接?

c# - 为什么.NET 中的事件使用发送者作为参数?

c# - 滚动您自己的 SQL 报告的最佳实践?

c# - 断开用户与托管网络的连接

c# - MVC 中的 Unity DI 有什么好处?

c# - C# 中的三元运算符

c# - C# 中的共享对象用于 python 脚本

javascript - 如何将 CSS(特别是 padding/margin )应用于选项或选择下拉列表的选项组?

c# - 将子域映射到 ASP.Net Core 3 中的区域

c# - 部分 View 不会在 ajax.beginform 之后更新