System.Uri.GetLeftPart C# (CSharp) Method

GetLeftPart() public method

public GetLeftPart ( UriPartial part ) : string
part UriPartial
return string
        public string GetLeftPart(UriPartial part)
        {
            if (IsNotAbsoluteUri)
            {
                throw new InvalidOperationException(SR.net_uri_NotAbsolute);
            }

            EnsureUriInfo();
            const UriComponents NonPathPart = (UriComponents.Scheme | UriComponents.UserInfo | UriComponents.Host | UriComponents.Port);

            switch (part)
            {
                case UriPartial.Scheme:

                    return GetParts(UriComponents.Scheme | UriComponents.KeepDelimiter, UriFormat.UriEscaped);

                case UriPartial.Authority:

                    if (NotAny(Flags.AuthorityFound) || IsDosPath)
                    {
                        // V1.0 compatibility.
                        // It not return an empty string but instead "scheme:" because it is a LEFT part.
                        // Also neither it should check for IsDosPath here

                        // From V1.0 comments:

                        // anything that didn't have "//" after the scheme name
                        // (mailto: and news: e.g.) doesn't have an authority
                        //

                        return string.Empty;
                    }
                    return GetParts(NonPathPart, UriFormat.UriEscaped);

                case UriPartial.Path:
                    return GetParts(NonPathPart | UriComponents.Path, UriFormat.UriEscaped);

                case UriPartial.Query:
                    return GetParts(NonPathPart | UriComponents.Path | UriComponents.Query, UriFormat.UriEscaped);
            }
            throw new ArgumentException(SR.Format(SR.Argument_InvalidUriSubcomponent, part), nameof(part));
        }

Usage Example

		static void createHttpRequest(IFakeAccessor context)
		{
			// MvcContrib.FakeHttpRequest doesn't implement RawUrl.
			var uri = new Uri(_currentUrl);
			context.The<HttpRequestBase>().WhenToldTo(x => x.Url).Return(uri);
			context.The<HttpRequestBase>().WhenToldTo(x => x.RawUrl)
				.Return(uri.AbsoluteUri.Substring(uri.GetLeftPart(UriPartial.Authority).Length));
			// Used by PathHelpers.GenerateClientUrl(...)
			context.The<HttpRequestBase>().WhenToldTo(x => x.ApplicationPath).Return("/");
		}
All Usage Examples Of System.Uri::GetLeftPart