public void AppendPackageParam_GivenUrl_VerifyThePackageNameIsAppendedCorrectly() { //Arrange: Initialize the PackagesManager and create variables holding the package name and fake URL to which the package will be appended var packageManager = new PackagesManager(); string urlWithParamters = @"http://fakedomain.org/homePage?fakeParam=0"; string urlWithNoParamters = @"http://fakedomain.org/homePage"; string packageName = "fakePackageName"; string appendedUrhWithParams; string appendedUrWithNoParams; string appendedUrlWithEmptyPackagename; //Act: append the package name appendedUrlWithEmptyPackagename = packageManager.AppendPackageParam(urlWithNoParamters, null); appendedUrhWithParams = packageManager.AppendPackageParam(urlWithParamters, packageName); appendedUrWithNoParams = packageManager.AppendPackageParam(urlWithNoParamters, packageName); //Assert: Verify the package name is properly appended Assert.AreEqual<string>(urlWithNoParamters, appendedUrlWithEmptyPackagename, "The URL must not be changed due to empty package name passed as parameter"); Assert.AreEqual<string>(string.Format("http://fakedomain.org/homePage?fakeParam=0&package={0}", packageName), appendedUrhWithParams, "The package name was not appended correctly as a second parameter"); Assert.AreEqual<string>(string.Format("http://fakedomain.org/homePage?package={0}", packageName), appendedUrWithNoParams, "The package name was not appended correctly as a parameter"); }
/// <summary> /// Resolves URL based on the current widget. /// </summary> /// <param name="helper">The URL helper.</param> /// <param name="contentPath">The content path.</param> /// <returns>Resolved URL.</returns> /// <exception cref="System.ArgumentNullException">contentPath</exception> /// <exception cref="System.InvalidOperationException"> /// Could not resolve the given URL because RouteData of the current context is null. /// or /// Could not resolve the given URL because RouteData does not contain \controller\ key. /// </exception> public static string WidgetContent(this UrlHelper helper, string contentPath) { var packagesManager = new PackagesManager(); if (contentPath.IsNullOrEmpty()) { throw new ArgumentNullException("contentPath"); } if (contentPath.StartsWith("~") || contentPath.StartsWith("/") || contentPath.Contains("://")) { return helper.Content(contentPath); } if (helper.RequestContext.RouteData == null) { throw new InvalidOperationException("Could not resolve the given URL because RouteData of the current context is null."); } string controllerName; if (helper.RequestContext.RouteData.Values.ContainsKey("controller")) { controllerName = (string)helper.RequestContext.RouteData.Values["controller"]; } else { throw new InvalidOperationException("Could not resolve the given URL because RouteData does not contain \"controller\" key."); } var controllerType = FrontendManager.ControllerFactory.ResolveControllerType(controllerName); var widgetPath = FrontendManager.VirtualPathBuilder.GetVirtualPath(controllerType); var packageName = packagesManager.GetCurrentPackage(); //"widgetName" is a parameter in the route of the Designer. It allows us to have a special fallback logic //where we first check for the requested resource in the widget assembly and then fallback to the current controller assembly. object widgetName; if (helper.RequestContext.RouteData.Values.TryGetValue("widgetName", out widgetName)) { controllerType = FrontendManager.ControllerFactory.ResolveControllerType((string)widgetName); var alternatePath = FrontendManager.VirtualPathBuilder.GetVirtualPath(controllerType); alternatePath = packagesManager.AppendPackageParam("~/" + alternatePath + contentPath, packageName); if (HostingEnvironment.VirtualPathProvider == null || HostingEnvironment.VirtualPathProvider.FileExists(alternatePath)) { return helper.Content(alternatePath); } } var resolvedPath = packagesManager.AppendPackageParam("~/" + widgetPath + contentPath, packageName); //If no resource is found for the current widget virtual path then get URL for Telerik.Sitefinity.Frontend. if (HostingEnvironment.VirtualPathProvider == null || HostingEnvironment.VirtualPathProvider.FileExists(resolvedPath)) { return helper.Content(resolvedPath); } else { resolvedPath = packagesManager.AppendPackageParam("~/" + FrontendManager.VirtualPathBuilder.GetVirtualPath(typeof(UrlHelpers).Assembly) + contentPath, packageName); return helper.Content(resolvedPath); } }