Try our conversational search powered by Generative AI!

Remove {node} segment in the URL

Vote:
 

Hi, 

We want to remove the {node} segment in the url so that the url will look shorter and easy to type :)

For example the current url is: www.mysite.com/article/2013.03/20/myarticle we want it to something like www.mysite.com/article/myarticle

any help will be greatly apreciated

Thanks,

#68317
Mar 20, 2013 5:40
Vote:
 

Hello,

You can register you're own route that includes you're own segment. Johan Björnfot has answered an forum post similar to this. Look here: http://world.episerver.com/Modules/Forum/Pages/Thread.aspx?id=65422

#68319
Mar 20, 2013 8:39
Vote:
 

Thank you Jonas,

In my case 2013.03/20/ is the part of the tree structure, it's not the parameter to load the pagedata. Also, is it possible apply the route to spectific pagetype?

Thank you very much!

#68323
Mar 20, 2013 10:02
Vote:
 

An alternative could be to hook up to event ContentRoute.CreatedVirtualPath and modify the generated Url. 

This would probably be a good approach if you have an easy recogizable pattern of the url you want to modify.

Note: If you use patch 2 to CMS7 then the event argument contains the RouteValues where you can find the ContentReference for the routed item.

#68346
Mar 20, 2013 13:14
Vote:
 

Thank you very much Johan,

Could you please give us some more details about what to modify? A code example would be great!

Regards,

Hai

#68349
Mar 20, 2013 14:11
Vote:
 

Is there anyway I can customize the {node} segment .i.e. I want to remove some part of the {node } segment, or override the {node} segment with something like {mynode}?

 

#68380
Mar 21, 2013 9:55
Vote:
 

You could register your own route before the builtin ones. In your own route you could replace the segment handling {node} part with your own node implemetation. You could then e.g. create your own class that inherits EPiServer.Web.Routing.Segments.NodeSegment and have your logic there.

See the previous post pointed by Jonas to where you can find more info on how to do that.

#68381
Mar 21, 2013 10:14
Vote:
 

If the approach Johan suggested doesn't work for you and you only want to customize the segment at creation time (i.e. not removing it) then I would suggest looking at the UrlSegment.CreatingUrlSegment event.

#68382
Mar 21, 2013 10:23
Vote:
 

Thank you Johan and Ben!

I will give it a try and get back to you later.

Really appreciate yours help!

Regards,

Hai

 

#68384
Mar 21, 2013 10:52
Vote:
 

Hi,

I have tried to write a custom {node} segment, basically copy the code of the NodeSegment and register it before the built-in segments as below: 

routingParameters.SegmentMappings.Add("modnode", new CustomNodeSegment("modnode", UrlRewriteProvider.FriendlyUrlExtension, urlSegmentRouter, new LanguageSegmentMatcher(routingParameters.LanguageBranchRepository), routingParameters.ContentLoader, routingParameters.UpdateCurrentLanguage));


string name = "pages";
string str4 = "{modnode}/{partial}/{action}";
object obj3 = new
{
action = "index"
};
routes.MapContentRoute(name, str4, obj3);

base.RegisterRoutes(routes);

But it seems not run, the url still the same, one more question which method in NodeSegment should I override to put my own path? Maybe GetOutGoingurl?

#68386
Mar 21, 2013 11:23
Vote:
 

To handle outgoing links you should override GetVirtualPathSegment. But since you are constructing your own urls you need to handle incoming routing as well so you need to override e.g. GetIncomingNode as well.

#68390
Mar 21, 2013 12:29
Vote:
 

Thank you Johan,

I've created the custom segment as you suggested, however when we use it the startpage is move to the rootpage, we cannot view the startpage anymore, maybe I'm missing something :(.

Thank you very much!

#69116
Mar 22, 2013 9:06
Vote:
 

It sounds like the "ordinary" route does not get a chance to handle the request.

You can think of all registered routes as "chained". That is only if one route does not handle a request the following routes in the chain will get a chance to handle the request.

So when implementing an own route it is important to not "capture" request that you do not handle since then follwing routes will not get a chance to route the request.

#69130
Mar 22, 2013 11:20
Vote:
 

Thank you for the point Johan!

However I can still go to edit/admin mode as normal only view mode redirect me to the Root page. By the way, is there any chance for me to debug the custom segment? It seems that I cannot debug the file.

Thank you very much!

Hai

 

#69133
Mar 22, 2013 11:45
Vote:
 

Yes you should be able to debug your segment. It should be no different to degug from other code (like controller, codebehind etc)

#69136
Mar 22, 2013 12:36
Vote:
 

Thank you very much Johan,

I'm still not able to debug the segment. First I want to debug the current {node} segment implementation of EPiServer, I copied it from the Reflector, rename, overide the RegisterRoutes method. I delete all the rest rules, but it seems  not work. Is there anyway which I can debug the original {node} segment?

Thank you very much!

Hai 

#69167
Mar 25, 2013 4:05
Vote:
 

Also, How can I create an instance of the {node} segment with its parameters:

routingParameters.SegmentMappings.Add(CustomRoutingConstants.CustomNodeKey, new CustomNodeSegment(CustomRoutingConstants.CustomNodeKey, UrlRewriteProvider.FriendlyUrlExtension, urlSegmentRouter, new LanguageSegmentMatcher(routingParameters.LanguageBranchRepository), routingParameters.ContentLoader, routingParameters.UpdateCurrentLanguage)); 

I used some parameter from the reflector, but I think it does not work, so some value was not added, this is how I did:

var routingParameters = new MapContentRouteParameters()
{
SegmentMappings = new Dictionary<string, ISegment>()
};
routingParameters.ServiceLocator = routingParameters.ServiceLocator ?? ServiceLocator.Current;
IRouteHandler routeHandler = routingParameters.RouteHandler;
if (routingParameters.ServiceLocator.AssignNullService<IRouteHandler>(ref routeHandler))
{
routingParameters.RouteHandler = routeHandler;
}
IUrlSegmentRouter urlSegmentRouter = routingParameters.UrlSegmentRouter;
if (routingParameters.ServiceLocator.AssignNullService<IUrlSegmentRouter>(ref urlSegmentRouter))
{
routingParameters.UrlSegmentRouter = urlSegmentRouter;
}
IRouteParser routeParser = routingParameters.RouteParser;
if (routingParameters.ServiceLocator.AssignNullService<IRouteParser>(ref routeParser))
{
routingParameters.RouteParser = routeParser;
}
ILanguageBranchRepository languageBranchRepository = routingParameters.LanguageBranchRepository;
if (
routingParameters.ServiceLocator.AssignNullService<ILanguageBranchRepository>(
ref languageBranchRepository))
{
routingParameters.LanguageBranchRepository = languageBranchRepository;
}
IViewRegistrator viewRegistrator = routingParameters.ViewRegistrator;
if (routingParameters.ServiceLocator.AssignNullService<IViewRegistrator>(ref viewRegistrator))
{
routingParameters.ViewRegistrator = viewRegistrator;
}
IContentLoader contentLoader = routingParameters.ContentLoader;
if (routingParameters.ServiceLocator.AssignNullService<IContentLoader>(ref contentLoader))
{
routingParameters.ContentLoader = contentLoader;
}
PartialRouteHandler partialRouteHandler = routingParameters.PartialRouteHandler;
if (routingParameters.ServiceLocator.AssignNullService<PartialRouteHandler>(ref partialRouteHandler))
{
routingParameters.PartialRouteHandler = partialRouteHandler;
}
TemplateResolver templateResolver = routingParameters.TemplateResolver;
if (routingParameters.ServiceLocator.AssignNullService<TemplateResolver>(ref templateResolver))
{
routingParameters.TemplateResolver = templateResolver;
}
IPermanentLinkMapper permanentLinkMapper = routingParameters.PermanentLinkMapper;
if (routingParameters.ServiceLocator.AssignNullService<IPermanentLinkMapper>(ref permanentLinkMapper))
{
routingParameters.PermanentLinkMapper = permanentLinkMapper;
}
IContentVersionRepository contentVersionRepository = routingParameters.ContentVersionRepository;
if (
routingParameters.ServiceLocator.AssignNullService<IContentVersionRepository>(
ref contentVersionRepository))
{
routingParameters.ContentVersionRepository = contentVersionRepository;
}
IUpdateCurrentLanguage updateCurrentLanguage = routingParameters.UpdateCurrentLanguage;
if (routingParameters.ServiceLocator.AssignNullService<IUpdateCurrentLanguage>(ref updateCurrentLanguage))
{
routingParameters.UpdateCurrentLanguage = updateCurrentLanguage;
}

 

#69168
Mar 25, 2013 4:26
Vote:
 

You should not need to reflect out the CMS code. Look at the code example at 

http://world.episerver.com/Modules/Forum/Pages/Thread.aspx?id=65422 

instead on how to register a route with a custom segment.

#69169
Mar 25, 2013 9:13
Vote:
 

Thank you!

Finally, It works :). It's my bad, I have somr error in declaring the custom segment. 

Thank you very much for your help! Really appreciate!

Regards,

Hai

 

#69172
Mar 25, 2013 10:35
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.