다음을 통해 공유


URI 매개 변수는 문자열이면 안 됩니다.

업데이트: 2007년 11월

TypeName

UriParametersShouldNotBeStrings

CheckId

CA1054

범주

Microsoft.Design

변경 수준

주요 변경

원인

형식이 이름에 "uri", "Uri", "urn", "Urn", "url" 또는 "Url"가 포함된 문자열 매개 변수가 있는 메서드를 선언한 다음 System.Uri 매개 변수를 사용하는 해당 오버로드를 선언하지 않습니다.

규칙 설명

이 규칙에서는 매개 변수 이름을 카멜식 대/소문자 규칙에 따라 토큰으로 분할하고 각 토큰이 "uri", "Uri", "urn", "Urn", "url" 또는 "Url"과 같은지 여부를 확인합니다. 일치하는 항목이 있으면 규칙에서는 매개 변수가 URI(Uniform Resource Identifier)를 나타낸다고 가정합니다. URI의 문자열 표현은 구문 분석 및 인코딩 오류를 발생시키기 쉬우며 보안 문제를 일으킬 수 있습니다. 메서드가 URI의 문자열 표현을 사용하면 Uri 클래스의 인스턴스를 사용하는 해당 오버로드를 제공해야 합니다. 이렇게 하면 서비스가 안전한 방식으로 제공됩니다.

위반 문제를 해결하는 방법

이 규칙 위반 문제를 해결하려면 매개 변수를 Uri 형식으로 변경합니다. 이것이 주요 변경에 해당합니다. 또는 Uri 매개 변수를 사용하는 메서드의 오버로드를 제공합니다. 이는 주요 변경에 해당하지 않습니다.

경고를 표시하지 않는 경우

매개 변수가 URI를 나타내지 않을 경우에는 이 규칙에서 경고를 표시하지 않아도 안전합니다.

예제

다음 예제에서는 이 규칙을 위반하는 ErrorProne 형식과 이 규칙을 만족하는 SaferWay 형식을 보여 줍니다.

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");
      }
   };
}

관련 규칙

URI 속성은 문자열이면 안 됩니다.

URI 반환 값은 문자열이면 안 됩니다.

문자열 대신 System.Uri 개체를 전달하십시오.

문자열 URI 오버로드는 System.Uri 오버로드를 호출합니다.