CA1054 : Les paramètres Uri ne doivent pas être des chaînes

TypeName

UriParametersShouldNotBeStrings

CheckId

CA1054

Catégorie

Microsoft.CSharp

Modification avec rupture

Oui

Cause

Un type déclare une méthode avec un paramètre de chaîne dont le nom contient "uri", "Uri", "urn", "Urn", "url" ou "Url", et le type ne déclare aucune surcharge correspondante qui accepte un paramètre System.Uri.

Description de la règle

Cette règle fractionne le nom de paramètres en jetons fondés sur la convention de casse mixte et contrôle si chaque jeton est égal à "uri", "Uri", "urn", "Urn", "url" ou "Url." En cas de correspondance, la règle considère que le paramètre représente un identificateur de ressources uniformes, ou URI (Uniform Resource Identifier). Une représentation sous forme de chaîne d'un URI est sujette aux erreurs d'analyse et d'encodage, et peut entraîner des failles de sécurité. Si une méthode accepte une représentation sous forme de chaîne d'un URI, une surcharge correspondante qui accepte une instance de la classe Uri doit être fournie ; elle-même fournit ces services de manière sûre et sécurisée.

Comment corriger les violations

Pour corriger une violation de cette règle, changez le paramètre en un type Uri ; il s'agit d'une modification avec rupture. Vous pouvez également fournir une surcharge de la méthode qui accepte un paramètre Uri ; il s'agit d'une modification sans rupture.

Quand supprimer les avertissements

Il est possible de supprimer sans risque un avertissement de cette règle si le paramètre ne représente pas une URI.

Exemple

L'exemple suivant affiche un type, ErrorProne, qui enfreint cette règle et un autre, SaferWay, qui la satisfait.

Imports System

Namespace DesignLibrary

   Public Class ErrorProne

      Dim someUriValue As String 

      ' Violates rule UriPropertiesShouldNotBeStrings.
      Property SomeUri As String
         Get 
            Return someUriValue 
         End Get
         Set 
            someUriValue = Value 
         End Set
      End Property

      ' Violates rule UriParametersShouldNotBeStrings.
      Sub AddToHistory(uriString As String)
      End Sub

      ' Violates rule UriReturnValuesShouldNotBeStrings.
      Function GetRefererUri(httpHeader As String) As String
         Return "https://www.adventure-works.com"
      End Function

   End Class

   Public Class SaferWay

      Dim someUriValue As Uri 

      ' To retrieve a string, call SomeUri.ToString().
      ' To set using a string, call SomeUri = New Uri(string).
      Property SomeUri As Uri
         Get 
            Return someUriValue 
         End Get
         Set 
            someUriValue = Value 
         End Set
      End Property

      Sub AddToHistory(uriString As String)
         ' Check for UriFormatException.
         AddToHistory(New Uri(uriString))
      End Sub

      Sub AddToHistory(uriString As Uri)
      End Sub

      Function GetRefererUri(httpHeader As String) As Uri
         Return New Uri("https://www.adventure-works.com")
      End Function

   End Class

End Namespace
using System;

namespace DesignLibrary
{
   public class ErrorProne
   {
      string someUri;

      // Violates rule UriPropertiesShouldNotBeStrings.
      public string SomeUri
      {
         get { return someUri; }
         set { someUri = value; }
      }

      // Violates rule UriParametersShouldNotBeStrings.
      public void AddToHistory(string uriString) { }

      // Violates rule UriReturnValuesShouldNotBeStrings.
      public string GetRefererUri(string httpHeader)
      {
         return "https://www.adventure-works.com";
      }
   }

   public class SaferWay
   {
      Uri someUri;

      // To retrieve a string, call SomeUri.ToString().
      // To set using a string, call SomeUri = new Uri(string).
      public Uri SomeUri
      {
         get { return someUri; }
         set { someUri = value; }
      }

      public void AddToHistory(string uriString)
      {
         // Check for UriFormatException.
         AddToHistory(new Uri(uriString));
      }

      public void AddToHistory(Uri uriType) { }

      public Uri GetRefererUri(string httpHeader)
      {
         return new Uri("https://www.adventure-works.com");
      }
   }
}
#using <system.dll>
using namespace System;

namespace DesignLibrary
{
   public ref class ErrorProne
   {
   public:
      // Violates rule UriPropertiesShouldNotBeStrings.
      property String^ SomeUri;

      // Violates rule UriParametersShouldNotBeStrings.
      void AddToHistory(String^ uriString) { }

      // Violates rule UriReturnValuesShouldNotBeStrings.
      String^ GetRefererUri(String^ httpHeader)
      {
         return "https://www.adventure-works.com";
      }
   };

   public ref class SaferWay
   {
   public:
      // To retrieve a string, call SomeUri()->ToString().
      // To set using a string, call SomeUri(gcnew Uri(string)).
      property Uri^ SomeUri;

      void AddToHistory(String^ uriString)
      {
         // Check for UriFormatException.
         AddToHistory(gcnew Uri(uriString));
      }

      void AddToHistory(Uri^ uriType) { }

      Uri^ GetRefererUri(String^ httpHeader)
      {
         return gcnew Uri("https://www.adventure-works.com");
      }
   };
}

Règles connexes

CA1056 : Les propriétés Uri ne doivent pas être des chaînes

CA1055 : Les valeurs de retour Uri ne doivent pas être des chaînes

CA2234 : Passez des objets System.Uri à la place de chaînes

CA1057 : Les surcharges d'uri de chaîne appellent les surcharges de System.Uri