plippard
plippard

Reputation: 385

UrlHelper.GenerateUrl

Can you provide a sample of using Urlhelper.GenerateUrl within an Action Filter using only the target controller and action ?

Upvotes: 17

Views: 18450

Answers (2)

Moises Ribeiro
Moises Ribeiro

Reputation: 61

You can use Url.Action( Action , Controller ).

Here an example to be more clear.

<ul>

    <li><a href="@Url.Action("Details" , "Reports", new { Id = report.Id })">
        <img src="~/Content/icons/view.svg" /></a></li>

    <li><a href="@Url.Action("Edit" , "Reports", new { Id = report.Id })">
        <img src="~/Content/icons/edit.svg" /></a></li>

    <li><a href="@Url.Action("Delete" , "Reports", new { Id = report.Id })">
        <img src="~/Content/icons/remove.svg" /></a></li>

</ul>

Upvotes: 5

Timeless
Timeless

Reputation: 7537

var url = UrlHelper.GenerateUrl(null, "action", "controller", null, RouteTable.Routes, HttpContext.Current.Request.RequestContext, false);

or

var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);    
var url = urlHelper.Action("action", "controller");

Upvotes: 40

Related Questions