asp.net mvc - Sitecore MVC routing to resolve a hyphenated name -
i trying sitecore mvc routing give me content path of
/home/test url/with space
from url:
/test-url/with-space
so need sitecore able recognise url has hyphen, can still resolve path without hyphen.
here have far:
routes.maproute( name: "hyphenated url", url: "test-url/{valuename}", defaults: new { scitempath = "/home/test url/{valuename}" });
this works path of:
/test-url/with space
but if add together hyphen in between "with" , "space", can't resolve. i'm guessing need custom route processor. ideas? in advance.
you create custom route.
routes.maphyphenatedroute( name: "hyphenated url", url: "{controller}/{action}", defaults: new { scitempath = "/home/{controller}/{action}" } );
the thought here intercept route values hyphens , translate them desired format. urls without hyphens remain untouched. sitecore still need ability handle scitempath
.
public static class routecollectionextensions { public static route maphyphenatedroute(this routecollection routes, string name, string url, object defaults) { var route = new hyphenatedroute(url, defaults); routes.add(name, route); homecoming route; } } public class hyphenatedroute : route { public hyphenatedroute(string url, object defaults) : base(url, new routevaluedictionary(defaults), new mvcroutehandler()) { } public override routedata getroutedata(system.web.httpcontextbase httpcontext) { var routedata = base.getroutedata(httpcontext); if (routedata != null) { routevaluedictionary values = routedata.values; values["controller"] = dehyphenate(values["controller"] string); values["action"] = dehyphenate(values["action"] string); } homecoming routedata; } public override virtualpathdata getvirtualpath(requestcontext ctx, routevaluedictionary values) { values["controller"] = hyphenate((values["controller"] string)); values["action"] = hyphenate(values["action"] string); homecoming base.getvirtualpath(ctx, values); } private static string hyphenate(string value) { if (!string.isnullorempty(value) && value.indexof(' ') >= 0) { homecoming system.globalization.cultureinfo.currentculture.textinfo.tolower(value.replace(' ', '-')); } homecoming value; } private static string dehyphenate(string value) { if (!string.isnullorempty(value) && value.indexof('-') >= 0) { homecoming system.globalization.cultureinfo.currentculture.textinfo.totitlecase(value.replace('-', ' ')); } homecoming value; } }
your scenario may need different handling, code might help if need route translation.
asp.net-mvc sitecore sitecore6
No comments:
Post a Comment