« Previous Next »

Thread: How to: Automating configuration of Error Pages with C#

Last post 02-13-2009 3:46 PM by Bressan. 4 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (5 items)

Sort Posts:

  • 02-12-2009, 3:45 PM

    • Bressan
    • Not Ranked
    • Joined on 02-12-2009, 3:03 PM
    • Posts 3

    How to: Automating configuration of Error Pages with C#

    Hi, I need configure Error Pages in a website programatically in C# on IIS7.

    I want customize the common erros, like 403, 404, 500, etc. with personalizated files, by editing the path of errors on "Error Pages" item of IIS Manager.

    If you can help me, I will be happy! =)

     Thank's.

  • 02-12-2009, 4:53 PM In reply to

    • ksingla
    • Top 25 Contributor
    • Joined on 06-14-2006, 3:02 AM
    • Redmond, WA
    • Posts 863

    Re: How to: Automating configuration of Error Pages with C#

    You have bunch of options. You can program against IIS WMI provider or use AhAdmin or MWA or even use DirectoryServices. See ways to automate IIS management here.

    Thanks.
    Kanwal

    Follow me on twitter at http://twitter.com/kjsingla
  • 02-12-2009, 5:27 PM In reply to

    • Bressan
    • Not Ranked
    • Joined on 02-12-2009, 3:03 PM
    • Posts 3

    Re: How to: Automating configuration of Error Pages with C#

    I'm using MWA, many functions are already implementeds(create website, ApplitacionPool, HttpRedirect, Default Documents, etc.), and only the configuration of Error Pages is missing. I have tried find a script to use, but nothing found.

     I need a script with MWA.

  • 02-12-2009, 6:31 PM In reply to

    • ksingla
    • Top 25 Contributor
    • Joined on 06-14-2006, 3:02 AM
    • Redmond, WA
    • Posts 863

    Re: How to: Automating configuration of Error Pages with C#

    You can generate the code you need using configuration editor. You can download it from here. Here is the code I got for editing custom errors entries.

    using System;

    using System.Text;

    using Microsoft.Web.Administration;

    internal static class Sample {private static void Main() {

     

    using(ServerManager serverManager = new ServerManager()) {

    Configuration config = serverManager.GetApplicationHostConfiguration();

     

    ConfigurationSection httpErrorsSection = config.GetSection("system.webServer/httpErrors", "Default Web Site");

     

    ConfigurationElementCollection httpErrorsCollection = httpErrorsSection.GetCollection();

     

    ConfigurationElement errorElement = FindElement(httpErrorsCollection, "error", "statusCode", @"403", "subStatusCode", @"-1");

    if (errorElement == null) throw new InvalidOperationException("Element not found!");

     

    errorElement["prefixLanguageFilePath"] = @"";

    errorElement["path"] = @"c:\mycustomerror\403.htm";

     

    serverManager.CommitChanges();

    }

    }

     

    private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues) {

    foreach (ConfigurationElement element in collection) {

    if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase)) {

    bool matches = true;

     

    for (int i = 0; i < keyValues.Length; i += 2) {

    object o = element.GetAttributeValue(keyValues[i]);

    string value = null;

    if (o != null) {

    value = o.ToString();

    }

     

    if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase)) {

    matches = false;

    break;

    }

    }

    if (matches) {

    return element;

    }

    }

    }

    return null;

    }

    }

    Thanks.
    Kanwal

    Follow me on twitter at http://twitter.com/kjsingla
  • 02-13-2009, 3:46 PM In reply to

    • Bressan
    • Not Ranked
    • Joined on 02-12-2009, 3:03 PM
    • Posts 3

    Re: How to: Automating configuration of Error Pages with C#

    Thank you! Your reply was very helpful!

Page 1 of 1 (5 items)
Microsoft Communities