How to get the current route in a view with ASP.NET 5 / MVC 6


Accessing the Current Route

I was wanting to access the current route from a shared layout page and then pass that route onto a controller using ASP.NET 5/MVC 6 (which would effectively be the referring route). Being still in the release candidate status at this point there wasn't a lot of good information I could find about how to access this. Here is the key piece to access the current route from the view:

Razor Syntax

@Url.RouteUrl(ViewContext.RouteData.Values)

Link with TagHelper Example

<a asp-controller="User" asp-action="SetDefaultFavorite" asp-route-value="@Url.RouteUrl(ViewContext.RouteData.Values)">
    <i class="ace-icon fa fa-star">>/i>
</a>

The code to access this "value" (e.g. asp-route-value) from the action result would look like this:

C#

[Authorize]
[Route("set-default-favorite")]
public IActionResult SetDefaultFavorite(string value)
{
    // Do something here
}