本文整理匯總了C#中System.Web.Routing.Route.GetVirtualPath方法的典型用法代碼示例。如果您正苦於以下問題:C# Route.GetVirtualPath方法的具體用法?C# Route.GetVirtualPath怎麽用?C# Route.GetVirtualPath使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Web.Routing.Route
的用法示例。
在下文中一共展示了Route.GetVirtualPath方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: LinkTo
public static string LinkTo(this UrlHelper urlHelper, Route route, RouteValueDictionary routeValues)
{
//TODO: remove code dupe see HtmlLinkToExtensions
var requestCtx = urlHelper.RequestContext;
var httpCtx = requestCtx.HttpContext;
if (routeValues != null)
{
foreach (var d in route.Defaults)
{
if (!routeValues.ContainsKey(d.Key))
routeValues.Add(d.Key, d.Value);
}
}
else
{
routeValues = route.Defaults;
}
VirtualPathData vpd = route.GetVirtualPath(requestCtx, routeValues);
if (vpd == null)
return null;
return httpCtx.Request.ApplicationPath + vpd.VirtualPath;
}
示例2: GetRedirectHandler
private static IHttpHandler GetRedirectHandler(RequestContext requestContext, string targetUrl, bool permanently)
{
string prefix = string.Empty;
var slashPosition = targetUrl.IndexOf('/');
if (slashPosition >= 0)
{
prefix = targetUrl.Substring(0, slashPosition + 1);
targetUrl = targetUrl.Substring(slashPosition + 1);
}
var route = new Route(targetUrl, null);
var virtualPathData = route.GetVirtualPath(requestContext, requestContext.RouteData.Values);
if (virtualPathData != null)
{
targetUrl = prefix + virtualPathData.VirtualPath;
}
return new DelegateHttpHandler(httpContext => Redirect(httpContext, targetUrl, permanently), false);
}
示例3: GetClientUrl
public string GetClientUrl(string url, RouteValueDictionary values)
{
var site = _frontContext.Site;
var subFolder = site.MatchedUrl.GetSubFolder();
if (!String.IsNullOrWhiteSpace(subFolder))
{
url = subFolder + "/" + url;
}
var route = new Route(url, new MvcRouteHandler());
var data = route.GetVirtualPath(HttpContext.Current.Request.RequestContext, values);
if (data == null)
{
return "/";
}
return "/" + data.VirtualPath;
}
示例4: GenerateTargetLocation
private static string GenerateTargetLocation(this RequestContext context, string target, bool permanent)
{
if (target.StartsWith("~/", StringComparison.Ordinal))
{
Route route = new Route(target.Substring(2), null);
var vpd = route.GetVirtualPath(context, context.RouteData.Values);
if (vpd != null)
return string.Format(CultureInfo.InvariantCulture, "{0}/{1}", permanent ? string.Empty : "~", vpd.VirtualPath);
}
else if (target.StartsWith("/", StringComparison.Ordinal))
{
Route route = new Route(target.Substring(1), null);
var vpd = route.GetVirtualPath(context, context.RouteData.Values);
if (null != vpd)
return "/" + vpd.VirtualPath;
}
return target;
}
示例5: GenerateOutboundVirtualPath
/// <summary>
/// Generates an outbound url from the specified values.
/// </summary>
/// <param name="routeValues">The route values from which to generate the url</param>
/// <param name="route">The route to use to generate the url</param>
/// <returns>The virtual path data object</returns>
public static VirtualPathData GenerateOutboundVirtualPath(RouteValueDictionary routeValues, Route route)
{
HttpContextBase mockHttp = MockHelpers.HttpContext(null).Object;
RequestContext context = new RequestContext(mockHttp, new RouteData());
// Act (generate a URL)
return route.GetVirtualPath(context, routeValues);
}
示例6: GetVirtualPath2
public void GetVirtualPath2 ()
{
var r = new Route ("{foo}/{bar}", null);
var hc = new HttpContextStub ("~/x/y", String.Empty);
var rd = r.GetRouteData (hc);
var vp = r.GetVirtualPath (new RequestContext (hc, rd), null);
Assert.IsNotNull (vp, "#1");
Assert.AreEqual ("x/y", vp.VirtualPath, "#2");
Assert.AreEqual (r, vp.Route, "#3");
Assert.AreEqual (0, vp.DataTokens.Count, "#4");
}
示例7: GetVirtualPath
public void GetVirtualPath ()
{
var r = new Route ("foo/bar", null);
var rd = new RouteData ();
var vp = r.GetVirtualPath (new RequestContext (new HttpContextStub ("~/foo/bar"), rd), null);
Assert.AreEqual ("foo/bar", vp.VirtualPath, "#1");
Assert.AreEqual (r, vp.Route, "#2");
vp = r.GetVirtualPath (new RequestContext (new HttpContextStub ("~/foo/bar/baz"), rd), null);
Assert.AreEqual ("foo/bar", vp.VirtualPath, "#3");
Assert.AreEqual (r, vp.Route, "#4");
}
示例8: GetVirtualPathNullValues
public void GetVirtualPathNullValues ()
{
// null values is allowed.
var r = new Route (null, null);
var rd = new RouteData ();
var vp = r.GetVirtualPath (new RequestContext (new HttpContextStub (), rd), null);
Assert.AreEqual (String.Empty, vp.VirtualPath, "#1");
Assert.AreEqual (r, vp.Route, "#2");
}
示例9: GetVirtualPathNullContext
public void GetVirtualPathNullContext ()
{
try {
var r = new Route (null, null);
r.GetVirtualPath (null, new RouteValueDictionary ());
} catch (NullReferenceException) {
// .NET lacks null arg check here. (No need to mimic silly behavior here.)
throw new ArgumentNullException ();
}
}
示例10: GetVirtualPath19
public void GetVirtualPath19 ()
{
var context = new HttpContextWrapper (
new HttpContext (new HttpRequest ("filename", "http://localhost/filename", String.Empty),
new HttpResponse (new StringWriter())
)
);
var rc = new RequestContext (context, new RouteData ());
var route = new Route ("HelloWorld", new MyRouteHandler ()) {
Defaults = new RouteValueDictionary (new {controller = "Home", action = "HelloWorld"})
};
var lowercase = route.GetVirtualPath (rc, new RouteValueDictionary
{
{"controller", "home"},
{"action", "helloworld"}
});
var standardCase = route.GetVirtualPath (rc, new RouteValueDictionary
{
{"controller", "Home"},
{"action", "HelloWorld"}
});
var uppercase = route.GetVirtualPath (rc, new RouteValueDictionary
{
{"controller", "HOME"},
{"action", "HELLOWORLD"}
});
Assert.IsNotNull(lowercase, "#A1");
Assert.AreEqual ("HelloWorld", lowercase.VirtualPath, "#A2");
Assert.IsNotNull(standardCase, "#A3");
Assert.AreEqual ("HelloWorld", standardCase.VirtualPath, "#A4");
Assert.IsNotNull(uppercase, "#A5");
Assert.AreEqual ("HelloWorld", uppercase.VirtualPath, "#A6");
}
示例11: GetVirtualPath16
public void GetVirtualPath16 ()
{
var context = new HttpContextWrapper (
new HttpContext (new HttpRequest ("filename", "http://localhost/filename", String.Empty),
new HttpResponse (new StringWriter())
)
);
var rc = new RequestContext (context, new RouteData ());
var route = new Route ("Hello", new MyRouteHandler ()) {
Defaults = new RouteValueDictionary (new {controller = "Home", action = "Hello", page = 1})
};
var routeValues = new RouteValueDictionary
{
{"controller", "Home"},
{"action", "Hello"},
{"page", 1}
};
var result = route.GetVirtualPath(rc, routeValues);
Assert.IsNotNull(result, "#A1");
Assert.AreEqual("Hello", result.VirtualPath, "#A2");
}
示例12: GetHttpHandler
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
if (_redirectParams.RedirectTo.StartsWith("~/"))
{
string virtualPath = _redirectParams.RedirectTo.Substring(2);
Route route = new Route(virtualPath, null);
var vpd = route.GetVirtualPath(requestContext, requestContext.RouteData.Values);
if (vpd != null)
{
_redirectParams.RedirectTo = "~/" + vpd.VirtualPath;
}
}
return new RedirectHandler(_redirectParams, false);
}