/// <summary> /// Registers the Http Handler in configuration. /// </summary> public virtual void RegisterHandlerInConfiguration() { // supported only at design time if (!this.DesignMode) { return; } // no point to add if http handler name or type is null if (string.IsNullOrEmpty(this.HttpHandlerName) || (null == this.HttpHandlerType)) { return; } // get the Web application configuration IWebApplication webApplication = (IWebApplication)this.Site.GetService(typeof(IWebApplication)); if (null != webApplication) { global::System.Configuration.Configuration webConfig = webApplication.OpenWebConfiguration(false); if (null == webConfig) { throw new ConfigurationErrorsException("web.config file not found to register the http handler."); } // get the <system.web> section ConfigurationSectionGroup systemWeb = webConfig.GetSectionGroup("system.web"); if (null == systemWeb) { systemWeb = new ConfigurationSectionGroup(); webConfig.SectionGroups.Add("system.web", systemWeb); } // get the <httpHandlers> section HttpHandlersSection httpHandlersSection = (HttpHandlersSection)systemWeb.Sections.Get("httpHandlers"); if (null == httpHandlersSection) { httpHandlersSection = new HttpHandlersSection(); systemWeb.Sections.Add("httpHandlers", httpHandlersSection); } // add the image handler httpHandlersSection.Handlers.Add(new HttpHandlerAction(this.HttpHandlerName, this.HttpHandlerType.AssemblyQualifiedName, "*")); // save the new web config webConfig.Save(); } }
private IEnumerable<ConfigurationHttpHandlersModel> ProcessHttpHandler(HttpHandlersSection httpHandlersSection) { if (httpHandlersSection == null) { return null; } var result = new List<ConfigurationHttpHandlersModel>(); foreach (HttpHandlerAction httpModule in httpHandlersSection.Handlers) { var resultItem = new ConfigurationHttpHandlersModel(); resultItem.Path = httpModule.Path; resultItem.Verb = httpModule.Verb; resultItem.Validate = httpModule.Validate; resultItem.Type = httpModule.Type; result.Add(resultItem); } return result; }
public UsingHttpHandlersSection() { // Process the // System.Web.Configuration.HttpHandlersSectionobject. try { // <Snippet1> // Get the Web application configuration. System.Configuration.Configuration configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/aspnetTest"); // Get the section. System.Web.Configuration.HttpHandlersSection httpHandlersSection = (System.Web.Configuration.HttpHandlersSection)configuration.GetSection("system.web/httphandlers"); // </Snippet1> // <Snippet2> // Get the handlers. System.Web.Configuration.HttpHandlerActionCollection httpHandlers = httpHandlersSection.Handlers; // </Snippet2> // <Snippet3> // Add a new HttpHandlerAction to the Handlers property HttpHandlerAction collection. httpHandlersSection.Handlers.Add(new HttpHandlerAction( "Calculator.custom", "Samples.Aspnet.SystemWebConfiguration.Calculator, CalculatorHandler", "GET", true)); // </Snippet3> // <Snippet4> // Get a HttpHandlerAction in the Handlers property HttpHandlerAction collection. HttpHandlerAction httpHandler = httpHandlers[0]; // </Snippet4> // <Snippet5> // Change the Path for the HttpHandlerAction. httpHandler.Path = "Calculator.custom"; // </Snippet5> // <Snippet6> // Change the Type for the HttpHandlerAction. httpHandler.Type = "Samples.Aspnet.SystemWebConfiguration.Calculator, CalculatorHandler"; // </Snippet6> // <Snippet7> // Change the Verb for the HttpHandlerAction. httpHandler.Verb = "POST"; // </Snippet7> // <Snippet8> // Change the Validate for the HttpHandlerAction. httpHandler.Validate = false; // </Snippet8> // <Snippet9> // Get the specified handler's index. HttpHandlerAction httpHandler2 = new HttpHandlerAction( "Calculator.custom", "Samples.Aspnet.SystemWebConfiguration.Calculator, CalculatorHandler", "GET", true); int handlerIndex = httpHandlers.IndexOf(httpHandler2); // </Snippet9> // <Snippet10> // Remove a HttpHandlerAction object HttpHandlerAction httpHandler3 = new HttpHandlerAction( "Calculator.custom", "Samples.Aspnet.SystemWebConfiguration.Calculator, CalculatorHandler", "GET", true); httpHandlers.Remove(httpHandler3); // </Snippet10> // <Snippet11> // Remove a HttpHandlerAction object with 0 index. httpHandlers.RemoveAt(0); // </Snippet11> // <Snippet12> // Clear all CustomError objects from the Handlers property HttpHandlerAction collection. httpHandlers.Clear(); // </Snippet12> // <Snippet13> // Remove the handler with the specified verb and path. httpHandlers.Remove("GET", "*.custom"); // </Snippet13> Console.WriteLine("List the Errors collection:"); int handlerActionCtr = 0; foreach (HttpHandlerAction handlerAction in httpHandlersSection.Handlers) { // <Snippet14> string type = handlerAction.Type; // </Snippet14> // <Snippet15> string verb = handlerAction.Verb; // </Snippet15> // <Snippet16> bool validation = handlerAction.Validate; // </Snippet16> Console.WriteLine(" {0}: message", ++handlerActionCtr); } // Update if not locked. if (!httpHandlersSection.SectionInformation.IsLocked) { configuration.Save(); Console.WriteLine("** Configuration updated."); } else { Console.WriteLine("** Could not update, section is locked."); } } catch (System.ArgumentException e) { // Unknown error. Console.WriteLine(e.ToString()); } }
internal static void CheckConfiguration(ISite site) { if (site == null) { return; } IWebApplication app = (IWebApplication)site.GetService(typeof(IWebApplication)); if (app == null) { return; } Configuration config = app.OpenWebConfiguration(false); HttpHandlersSection handlers = (HttpHandlersSection)config.GetSection("system.web/httpHandlers"); // Does the httpHandlers Secton already exist? if (handlers == null) { // If not, add it... handlers = new HttpHandlersSection(); ConfigurationSectionGroup group = config.GetSectionGroup("system.web"); // Does the system.web Section already exist? if (group == null) { // If not, add it... config.SectionGroups.Add("system.web", new ConfigurationSectionGroup()); group = config.GetSectionGroup("system.web"); } if (group != null) { group.Sections.Add("httpHandlers", handlers); } } HttpHandlerAction action = new HttpHandlerAction("*/ext.axd", "Ext.Net.ResourceHandler", "*", false); // Does the ResourceHandler already exist? if (handlers.Handlers.IndexOf(action) < 0) { // If not, add it... handlers.Handlers.Add(action); config.Save(); } HttpModulesSection modules = (HttpModulesSection)config.GetSection("system.web/httpModules"); // Does the httpModules Secton already exist? if (modules == null) { // If not, add it... modules = new HttpModulesSection(); ConfigurationSectionGroup group = config.GetSectionGroup("system.web"); // Does the system.web Section already exist? if (group == null) { // If not, add it... config.SectionGroups.Add("system.web", new ConfigurationSectionGroup()); group = config.GetSectionGroup("system.web"); } if (group != null) { group.Sections.Add("httpModules", modules); } } //<add name="DirectRequestModule" type="Ext.Net.DirectRequestModule, Ext.Net" /> HttpModuleAction action2 = new HttpModuleAction("DirectRequestModule", "Ext.Net.DirectRequestModule, Ext.Net"); // Does the ResourceHandler already exist? if (modules.Modules.IndexOf(action2) < 0) { // If not, add it... modules.Modules.Add(action2); config.Save(); } }