다음을 통해 공유


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

TypeName

UriReturnValuesShouldNotBeStrings

CheckId

CA1055

범주

Microsoft.Design

변경 수준

주요 변경

원인

메서드의 이름에 "uri", "Uri", "urn", "Urn", "url" 또는 "Url"이 포함되어 있고 메서드에서 문자열을 반환합니다.

규칙 설명

이 규칙에서는 메서드 이름을 파스칼식 대/소문자 구분 규칙에 따라 토큰으로 분할하고 각 토큰이 "uri", "Uri", "urn", "Urn", "url" 또는 "Url"과 같은지 여부를 확인합니다.일치하는 항목이 있으면 규칙에서는 메서드가 URI(Uniform Resource Identifier)를 나타낸다고 가정합니다.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");
      }
   };
}

관련 규칙

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

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

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

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