UrlMapping Classe

Définition

Mappe une URL affichée aux utilisateurs à l’URL d’une page dans votre application web. Cette classe ne peut pas être héritée.

public ref class UrlMapping sealed : System::Configuration::ConfigurationElement
public sealed class UrlMapping : System.Configuration.ConfigurationElement
type UrlMapping = class
    inherit ConfigurationElement
Public NotInheritable Class UrlMapping
Inherits ConfigurationElement
Héritage

Exemples

L’exemple suivant utilise le UrlMappingsSection fichier Web.config pour mapper deux URL et ajoute un mappage pour une URL supplémentaire. Lorsque vous modifiez et enregistrez le fichier Web.config, votre application redémarre.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Configuration" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    int showVal = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        // Get the parameter value from the QueryString
        if (Request.Params["show"] != null)
            showVal = Int32.Parse(Request.Params["show"]);

        // Show a page depending on the parameter value
        NoShowPanel.Visible = (showVal == 0);
        ShowHomePage.Visible = (showVal == 1);
        ShowProductsPage.Visible = (showVal == 2);
        ShowEventsPage.Visible = (showVal == 3);

        // <Snippet2>
        UrlMapping urlMap = null;
        
        // Open Web.config
        Configuration config =
            WebConfigurationManager.OpenWebConfiguration("~");
        // Get the UrlMappings section
        UrlMappingsSection urlMapSection =
            (UrlMappingsSection)config.GetSection(
                "system.web/urlMappings");
        
        // Modify UrlMapping in Web.config first time through
        if (!Page.IsPostBack)
        {
            // If not already added, add a UrlMapping to the section
            if (urlMapSection.UrlMappings.Count == 2)
            {
                urlMap = new UrlMapping("~/events.aspx", 
                    "~/default.aspx?show=3");
                urlMapSection.UrlMappings.Add(urlMap);
                
                // This line assumes permission to write to disk
                config.Save();
            }
        }

        if (showVal > 0)
        {
            // <Snippet4>
            urlMap = (UrlMapping)urlMapSection.UrlMappings[showVal - 1];
            realURL.Text = urlMap.MappedUrl;
            // </Snippet4>
        }
        // </Snippet2>
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>UrlMapping Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:Panel ID="NoShowPanel" runat="server" Visible="true">
        <h2>Show no page</h2>
        <p><a href="home.aspx">Home.aspx</a></p>
        <p><a href="products.aspx">Products.aspx</a></p>
        <p><a href="events.aspx">Events.aspx</a></p>
    </asp:Panel>
    <asp:Panel ID="ShowHomePage" runat="server" Visible="false">
        <h2>Home Page</h2>
        <p><a href="products.aspx">Products.aspx</a></p>
        <p><a href="events.aspx">Events.aspx</a></p>
    </asp:Panel>
    <asp:Panel ID="ShowProductsPage" runat="server" Visible="false">
        <h2>Products Page</h2>
        <p><a href="home.aspx">Home.aspx</a></p>
        <p><a href="events.aspx">Events.aspx</a></p>
    </asp:Panel>
    <asp:Panel ID="ShowEventsPage" runat="server" Visible="false">
        <h2>Events Page</h2>
        <p><a href="home.aspx">Home.aspx</a></p>
        <p><a href="products.aspx">Products.aspx</a></p>
    </asp:Panel>
    <p>The real URL for this page is 
        <asp:Label ID="realURL" runat="server">[None]</asp:Label></p>

    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Configuration" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Dim showVal As Integer

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        ' Get the parameter value from the QueryString
        If Not IsNothing(Request.Params("show")) Then
            showVal = Int32.Parse(Request.Params("show"))
        Else
            showVal = 0
        End If
        
        ' Show a page depending on the parameter value
        NoShowPanel.Visible = (showVal = 0)
        ShowHomePage.Visible = (showVal = 1)
        ShowProductsPage.Visible = (showVal = 2)
        ShowEventsPage.Visible = (showVal = 3)

        ' <Snippet2>
         dim urlMap as UrlMapping

        Dim config As Configuration
        ' Open Web.config
        config = _
            WebConfigurationManager.OpenWebConfiguration("~")
        ' Get the UrlMappings section
        Dim urlMapSection As UrlMappingsSection
        urlMapSection = _
           CType(config.GetSection( _
               "system.web/urlMappings"), UrlMappingsSection)
               
        ' Modify UrlMapping in Web.config first time through
        If (Not Page.IsPostBack) Then
            ' If not already added, add a UrlMapping to the section
            If urlMapSection.UrlMappings.Count = 2 Then
                urlMap = New UrlMapping("~/events.aspx", _
                    "~/default.aspx?show=3")
                urlMapSection.UrlMappings.Add(urlMap)
                
                ' This line assumes permission to write to disk
                config.Save()
            End If
        End If
        
        If showVal > 0 Then
            '<Snippet4>
            urlMap = CType(urlMapSection.UrlMappings(showVal - 1), UrlMapping)
            realURL.Text = urlMap.MappedUrl
            '</Snippet4>
        End If
        ' </Snippet2>
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>UrlMapping Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:Panel ID="NoShowPanel" runat="server" Visible="true">
        <h2>Show no page</h2>
        <p><a href="home.aspx">Home.aspx</a></p>
        <p><a href="products.aspx">Products.aspx</a></p>
        <p><a href="events.aspx">Events.aspx</a></p>
    </asp:Panel>
    <asp:Panel ID="ShowHomePage" runat="server" Visible="false">
        <h2>Home Page</h2>
        <p><a href="products.aspx">Products.aspx</a></p>
        <p><a href="events.aspx">Events.aspx</a></p>
    </asp:Panel>
    <asp:Panel ID="ShowProductsPage" runat="server" Visible="false">
        <h2>Products Page</h2>
        <p><a href="home.aspx">Home.aspx</a></p>
        <p><a href="events.aspx">Events.aspx</a></p>
    </asp:Panel>
    <asp:Panel ID="ShowEventsPage" runat="server" Visible="false">
        <h2>Events Page</h2>
        <p><a href="home.aspx">Home.aspx</a></p>
        <p><a href="products.aspx">Products.aspx</a></p>
    </asp:Panel>
    <p>The real URL for this page is 
        <asp:Label ID="realURL" runat="server">default.aspx</asp:Label></p>

    </div>
    </form>
</body>
</html>

Remarques

La UrlMapping classe vous permet de mapper une URL affichée aux utilisateurs à une URL qui existe dans votre application web. L’ajout d’un UrlMapping objet à un UrlMappingCollection est l’équivalent programmatique de l’inclusion d’un add élément dans la urlMappings section d’un fichier de configuration.

Chaque UrlMapping objet contient deux propriétés identifiant une URL. Une propriété spécifie l’URL affichée à l’utilisateur ; l’autre spécifie une URL dans votre application web. Les caractères d’espace blanc de fin sont ignorés dans les propriétés et Url les MappedUrl propriétés.

Note

La UrlMapping propriété peut écrire des informations dans la section associée du fichier de configuration en fonction des restrictions définies par la propriété de AllowDefinition section dont la valeur est MachineToApplication. Toute tentative d’écriture dans un fichier de configuration à un niveau non autorisé dans la hiérarchie entraîne un message d’erreur généré par l’analyseur. Toutefois, vous pouvez utiliser cette classe pour lire les informations de configuration à n’importe quel niveau de la hiérarchie.

Constructeurs

Nom Description
UrlMapping(String, String)

Initialise une nouvelle instance de la classe UrlMapping.

Propriétés

Nom Description
CurrentConfiguration

Obtient une référence à l’instance de niveau supérieur Configuration qui représente la hiérarchie de configuration à laquelle appartient l’instance ConfigurationElement actuelle.

(Hérité de ConfigurationElement)
ElementInformation

Obtient un objet ElementInformation qui contient les informations et fonctionnalités non personnalisables de l’objet ConfigurationElement.

(Hérité de ConfigurationElement)
ElementProperty

Obtient l’objet ConfigurationElementProperty qui représente l’objet ConfigurationElement lui-même.

(Hérité de ConfigurationElement)
EvaluationContext

Obtient l'objet ContextInformation pour l'objet ConfigurationElement.

(Hérité de ConfigurationElement)
HasContext

Obtient une valeur qui indique si la propriété CurrentConfiguration est null.

(Hérité de ConfigurationElement)
Item[ConfigurationProperty]

Obtient ou définit une propriété ou un attribut de cet élément de configuration.

(Hérité de ConfigurationElement)
Item[String]

Obtient ou définit une propriété, un attribut ou un élément enfant de cet élément de configuration.

(Hérité de ConfigurationElement)
LockAllAttributesExcept

Obtient la collection d’attributs verrouillés.

(Hérité de ConfigurationElement)
LockAllElementsExcept

Obtient la collection d’éléments verrouillés.

(Hérité de ConfigurationElement)
LockAttributes

Obtient la collection d’attributs verrouillés.

(Hérité de ConfigurationElement)
LockElements

Obtient la collection d’éléments verrouillés.

(Hérité de ConfigurationElement)
LockItem

Obtient ou définit une valeur indiquant si l’élément est verrouillé.

(Hérité de ConfigurationElement)
MappedUrl

URL dans votre application web.

Properties

Obtient la collection de propriétés.

(Hérité de ConfigurationElement)
Url

Obtient l’URL affichée à l’utilisateur.

Méthodes

Nom Description
DeserializeElement(XmlReader, Boolean)

Lit le code XML à partir du fichier de configuration.

(Hérité de ConfigurationElement)
Equals(Object)

Compare l’instance actuelle ConfigurationElement à l’objet spécifié.

(Hérité de ConfigurationElement)
GetHashCode()

Obtient une valeur unique représentant l’instance actuelle ConfigurationElement .

(Hérité de ConfigurationElement)
GetTransformedAssemblyString(String)

Retourne la version transformée du nom d’assembly spécifié.

(Hérité de ConfigurationElement)
GetTransformedTypeString(String)

Retourne la version transformée du nom de type spécifié.

(Hérité de ConfigurationElement)
GetType()

Obtient la Type de l’instance actuelle.

(Hérité de Object)
Init()

Définit l’objet ConfigurationElement à son état initial.

(Hérité de ConfigurationElement)
InitializeDefault()

Permet d’initialiser un jeu de valeurs par défaut pour l’objet ConfigurationElement.

(Hérité de ConfigurationElement)
IsModified()

Indique si cet élément de configuration a été modifié depuis son dernier enregistrement ou chargement, lorsqu’il est implémenté dans une classe dérivée.

(Hérité de ConfigurationElement)
IsReadOnly()

Obtient une valeur indiquant si l’objet ConfigurationElement est en lecture seule.

(Hérité de ConfigurationElement)
ListErrors(IList)

Ajoute les erreurs de propriété non valides dans cet objet ConfigurationElement et, dans tous les sous-éléments, à la liste passée.

(Hérité de ConfigurationElement)
MemberwiseClone()

Crée une copie superficielle du Objectactuel.

(Hérité de Object)
OnDeserializeUnrecognizedAttribute(String, String)

Obtient une valeur indiquant si un attribut inconnu est rencontré lors de la désérialisation.

(Hérité de ConfigurationElement)
OnDeserializeUnrecognizedElement(String, XmlReader)

Obtient une valeur indiquant si un élément inconnu est rencontré lors de la désérialisation.

(Hérité de ConfigurationElement)
OnRequiredPropertyNotFound(String)

Lève une exception lorsqu’une propriété requise est introuvable.

(Hérité de ConfigurationElement)
PostDeserialize()

Appelé après la désérialisation.

(Hérité de ConfigurationElement)
PreSerialize(XmlWriter)

Appelé avant la sérialisation.

(Hérité de ConfigurationElement)
Reset(ConfigurationElement)

Réinitialise l’état interne de l’objet ConfigurationElement , y compris les verrous et les collections de propriétés.

(Hérité de ConfigurationElement)
ResetModified()

Réinitialise la valeur de la IsModified() méthode false lorsqu’elle est implémentée dans une classe dérivée.

(Hérité de ConfigurationElement)
SerializeElement(XmlWriter, Boolean)

Écrit le contenu de cet élément de configuration dans le fichier de configuration en cas d’implémentation dans une classe dérivée.

(Hérité de ConfigurationElement)
SerializeToXmlElement(XmlWriter, String)

Écrit les balises externes de cet élément de configuration dans le fichier de configuration en cas d’implémentation dans une classe dérivée.

(Hérité de ConfigurationElement)
SetPropertyValue(ConfigurationProperty, Object, Boolean)

Définit une propriété sur la valeur spécifiée.

(Hérité de ConfigurationElement)
SetReadOnly()

Définit la IsReadOnly() propriété de l’objet ConfigurationElement et de tous les sous-éléments.

(Hérité de ConfigurationElement)
ToString()

Retourne une chaîne qui représente l’objet actuel.

(Hérité de Object)
Unmerge(ConfigurationElement, ConfigurationElement, ConfigurationSaveMode)

Modifie l’objet ConfigurationElement pour supprimer toutes les valeurs qui ne doivent pas être enregistrées.

(Hérité de ConfigurationElement)

S’applique à

Voir aussi