// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using Microsoft.AspNet.Http; using Microsoft.Framework.Internal; namespace Microsoft.AspNet.Routing { /// /// Represents information about the route and virtual path that are the result of /// generating a URL with the ASP.NET routing middleware. /// public class VirtualPathData { private readonly IDictionary _dataToken; /// /// Initializes a new instance of the class. /// /// The object that is used to generate the URL. /// The generated URL. public VirtualPathData([NotNull] IRouter router, string virtualPath) : this(router, virtualPath, dataTokens: new RouteValueDictionary()) { } /// /// Initializes a new instance of the class. /// /// The object that is used to generate the URL. /// The generated URL. /// The collection of custom values. public VirtualPathData( [NotNull] IRouter router, string virtualPath, IDictionary dataTokens) : this(router, CreatePathString(virtualPath), dataTokens) { } /// /// Initializes a new instance of the class. /// /// The object that is used to generate the URL. /// The generated URL. /// The collection of custom values. public VirtualPathData( [NotNull] IRouter router, PathString virtualPath, IDictionary dataTokens) { Router = router; VirtualPath = virtualPath; _dataToken = new RouteValueDictionary(); if (dataTokens != null) { foreach (var dataToken in dataTokens) { _dataToken.Add(dataToken.Key, dataToken.Value); } } } /// /// Gets the collection of custom values for the . /// public IDictionary DataTokens { get { return _dataToken; } } /// /// Gets or sets the that was used to generate the URL. /// public IRouter Router { get; set; } /// /// Gets or sets the URL that was generated from the . /// public PathString VirtualPath { get; set; } private static PathString CreatePathString(string path) { if (!string.IsNullOrEmpty(path)) { PathString pathString; if (path.Length > 0 && !path.StartsWith("/", StringComparison.Ordinal)) { pathString = new PathString("/" + path); } else { pathString = new PathString(path); } return pathString; } return PathString.Empty; } } }