/// <summary> /// Retrieves a particular routeurl from the global route table. Because the app is running inside OWIN host, we need to query route from the global routetable /// instead of route from OWIN configuration /// </summary> /// <returns>The url of route</returns> public static string RouteAppUrl(this UrlHelper urlHelper, string routeName, dynamic routeValues) { if (routeValues == null) { routeValues = new { } } ; try { return(urlHelper.RouteUrl(routeName, routeValues)); // RouteTable.Routes.GetVirtualPath(null, new RouteValueDictionary(routeValues)); } catch (Exception ex) { // ignored } //search in webapi routes IHttpRoute apiRoute; var vPath = WebApiConfig.Configuration.Routes.TryGetValue(routeName, out apiRoute); if (!vPath) { throw new mobSocialException(string.Format("Can't find a route named {0} in the RouteTable", routeName)); } return(apiRoute.RouteTemplate); }
// // [TestMethod] public void Identity_AccountController_UrlHelper_Test() { var obj = new { userId = "111-11111", code = "A12B==", httproute = true }; var _config = _sut.Request.GetConfiguration(); var _url = new System.Web.Mvc.UrlHelper(); var _out = _url.RouteUrl("ConfirmEmailRoute", obj); string _rUrlPart = "api/Account/ConfirmEmail" + _out; Assert.AreNotEqual("", _rUrlPart); }
//Builds URL by finding the best matching route that corresponds to the current URL, //with given parameters added or replaced. public static System.Web.Mvc.MvcHtmlString Current(this System.Web.Mvc.UrlHelper helper, object substitutes) { //get the route data for the current URL e.g. /Research/InvestmentModelling/RiskComparison //this is needed because unlike UrlHelper.Action, UrlHelper.RouteUrl sets includeImplicitMvcValues to false //which causes it to ignore current ViewContext.RouteData.Values var rd = new System.Web.Routing.RouteValueDictionary(helper.RequestContext.RouteData.Values); //get the current query string e.g. ?BucketID=17371&compareTo=123 var qs = helper.RequestContext.HttpContext.Request.QueryString; //add query string parameters to the route value dictionary foreach (string param in qs) { if (!string.IsNullOrEmpty(qs[param])) { rd[param] = qs[param]; } } //override parameters we're changing foreach ( System.ComponentModel.PropertyDescriptor property in System.ComponentModel.TypeDescriptor.GetProperties(substitutes.GetType())) { var value = property.GetValue(substitutes); if (value == null) { rd.Remove(property.Name); } else { rd[property.Name] = value; } } //UrlHelper will find the first matching route //(the routes are searched in the order they were registered). //The unmatched parameters will be added as query string. var url = helper.RouteUrl(rd); return(new System.Web.Mvc.MvcHtmlString(url)); }
public string RouteUrl(RouteValueDictionary routeValues) { var urlHelper = new System.Web.Mvc.UrlHelper(HttpContext.Current.Request.RequestContext); return(urlHelper.RouteUrl(routeValues)); }
public static string AbsoluteRouteUrl(this System.Web.Mvc.UrlHelper url, string routeName, object values) { Uri requestUrl = url.RequestContext.HttpContext.Request.Url; string absoluteRouteUrl = String.Format("{0}://{1}{2}", requestUrl.Scheme, requestUrl.Authority, url.RouteUrl(routeName, values)); return(absoluteRouteUrl); }