Surendra Sharma

Surendra Sharma

Search This Blog

Saturday, January 21, 2017

How to avoid “api/sitecore” from Sitecore MVC link URL


There was one Logout link which is working fine in our application, but its URL was looking very weird as http://sitecorelessons/api/sitecore/account/logout.

Client told us to remove that “/api/sitecore” part, even we are also thinking why this “api/sitecore” is coming in URL?

Why “api/sitecore” is coming?

We are generating hyper link with MVC ActionLink() method as 

@Html.ActionLink("Logout", "Logout", "Account", null, new { @class = "btn btn-logout" })

We had used View Rendering for logout section which uses the SitecoreController to render itself and that’s why URL is generated with the default route of Sitecore which ultimately include "api/sitecore" part.

How to solve this?

     1. Create map route for logout action method as

using Sitecore.Pipelines;

public class ConfigureRoutes
{
    public virtual void Process(PipelineArgs args)
    {
        RouteTable.Routes.MapRoute(
            name: "Accountlogout",
            url: "Account/Logout/{id}",
            defaults: new { controller = "Account", action = "Logout", id = UrlParameter.Optional }
            );
    }
}

    2. Point your Controller to move two folder up as

@Html.ActionLink("Logout", "Logout", "../../Account", null, new { @class = "btn btn-logout" })

That’s it. Now when I checked the link on page, it created correctly and working fine as

I hope you like this Sitecore tip. Stay tuned for more Sitecore related articles.

Till that happy sitecoring :)

Please leave your comments or share this article if it’s useful for you.

1 comment:

  1. Thanks for your post. Does this mean we need to hard-code every route if don't want to see /api/siticore?

    ReplyDelete