using System; using System.Linq; using System.Linq.Expressions; using System.Management.Instrumentation; using System.Web.Mvc; using Umbraco.Core; using Umbraco.Web.Mvc; using Umbraco.Web.WebApi; using Umbraco.Web.WebServices; namespace Umbraco.Web { /// /// Extension methods for UrlHelper /// public static class UrlHelperExtensions { /// /// Returns the base path (not including the 'action') of the MVC controller "ExamineManagementController" /// /// /// public static string GetExamineManagementServicePath(this UrlHelper url) { var result = url.GetUmbracoApiService("GetIndexerDetails"); return result.TrimEnd("GetIndexerDetails").EnsureEndsWith('/'); } /// /// Return the Url for a Web Api service /// /// /// /// /// public static string GetUmbracoApiService(this UrlHelper url, string actionName) where T : UmbracoApiController { return url.GetUmbracoApiService(actionName, typeof (T)); } /// /// Return the Base Url (not including the action) for a Web Api service /// /// /// /// /// public static string GetUmbracoApiServiceBaseUrl(this UrlHelper url, string actionName) where T : UmbracoApiController { return url.GetUmbracoApiService(actionName).TrimEnd(actionName); } public static string GetUmbracoApiServiceBaseUrl(this UrlHelper url, Expression> methodSelector) where T : UmbracoApiController { var method = Umbraco.Core.ExpressionHelper.GetMethodInfo(methodSelector); if (method == null) { throw new MissingMethodException("Could not find the method " + methodSelector + " on type " + typeof(T) + " or the result "); } return url.GetUmbracoApiService(method.Name).TrimEnd(method.Name); } public static string GetUmbracoApiService(this UrlHelper url, Expression> methodSelector) where T : UmbracoApiController { var method = Umbraco.Core.ExpressionHelper.GetMethodInfo(methodSelector); if (method == null) { throw new MissingMethodException("Could not find the method " + methodSelector + " on type " + typeof(T) + " or the result "); } return url.GetUmbracoApiService(method.Name); } /// /// Return the Url for a Web Api service /// /// /// /// /// public static string GetUmbracoApiService(this UrlHelper url, string actionName, Type apiControllerType) { Mandate.ParameterNotNullOrEmpty(actionName, "actionName"); Mandate.ParameterNotNull(apiControllerType, "apiControllerType"); var area = ""; var apiController = UmbracoApiControllerResolver.Current.RegisteredUmbracoApiControllers .SingleOrDefault(x => x == apiControllerType); if (apiController == null) throw new InvalidOperationException("Could not find the umbraco api controller of type " + apiControllerType.FullName); var metaData = PluginController.GetMetadata(apiController); if (!metaData.AreaName.IsNullOrWhiteSpace()) { //set the area to the plugin area area = metaData.AreaName; } return url.GetUmbracoApiService(actionName, ControllerExtensions.GetControllerName(apiControllerType), area); } /// /// Return the Url for a Web Api service /// /// /// /// /// public static string GetUmbracoApiService(this UrlHelper url, string actionName, string controllerName) { return url.GetUmbracoApiService(actionName, controllerName, ""); } /// /// Return the Url for a Web Api service /// /// /// /// /// /// public static string GetUmbracoApiService(this UrlHelper url, string actionName, string controllerName, string area) { Mandate.ParameterNotNullOrEmpty(controllerName, "controllerName"); Mandate.ParameterNotNullOrEmpty(actionName, "actionName"); return url.Action(actionName, controllerName, new { httproute = "", area = area }); } } }