In ASP.NET MVC, we have @Url.Action
for actions. Is there something similar like @Url.Api
which would route to /api/controller?

- 57,477
- 67
- 243
- 335

- 13,960
- 22
- 76
- 113
4 Answers
The ApiController has a property called Url which is of type System.Web.Http.Routing.UrlHelper which allows you to construct urls for api controllers.
Example:
public class ValuesController : ApiController
{
// GET /api/values
public IEnumerable<string> Get()
{
// returns /api/values/123
string url = Url.Route("DefaultApi", new { controller = "values", id = "123" });
return new string[] { "value1", "value2" };
}
// GET /api/values/5
public string Get(int id)
{
return "value";
}
...
}
This UrlHelper doesn't exist neither in your views nor in the standard controllers.
UPDATE:
And in order to do routing outside of an ApiController you could do the following:
public class HomeController : Controller
{
public ActionResult Index()
{
string url = Url.RouteUrl(
"DefaultApi",
new { httproute = "", controller = "values", id = "123" }
);
return View();
}
}
or inside a view:
<script type="text/javascript">
var url = '@Url.RouteUrl("DefaultApi", new { httproute = "", controller = "values", id = "123" })';
$.ajax({
url: url,
type: 'GET',
success: function(result) {
// ...
}
});
</script>
Notice the httproute = ""
route token which is important.
Obviously this assumes that your Api route is called DefaultApi
in your RegisterRoutes method in Global.asax
:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

- 1,023,142
- 271
- 3,287
- 2,928
-
That.. really doesn't help me much since I need my View to be able to generate Urls in a safe way. If there is a Go Live license what is the 'Go Live' way to create urls on the View? – Shane Courtrille Feb 29 '12 at 16:16
-
More importantly this won't work if you're using ASP.Net Development Server since it changes ports all the time so you can't even hardcore the url in the view. – Shane Courtrille Feb 29 '12 at 16:18
-
@ShaneCourtrille, I have updated my answer to illustrate how you could use the normal `System.Web.Mvc.UrlHelper` to generate Api routes outside of an HttpControllerContext. – Darin Dimitrov Feb 29 '12 at 16:39
-
Thanks very much! I was trying to use RouteUrl but didn't know about the httproute param requirement. – Shane Courtrille Feb 29 '12 at 17:28
-
@ShaneCourtrille, did you manage to get it working? Is there something else you would like to ask about this? – Darin Dimitrov Mar 01 '12 at 13:22
-
How could I use this if my API controller is inside my Admin area? – ProfK Apr 24 '12 at 07:08
-
is it possible to construct the UrlHelper outside of ApiController? looking for a way to do that. – Eatdoku May 07 '12 at 23:53
-
Where would this *outside* be? You can have access to an UrlHelper anywhere you have access to the HttpContext. If you need to access it in your business layer where there's no HttpContext then you are probably doing something wrong as the business layer is meant to be reusable and shouldn't depend on web specific classes. – Darin Dimitrov May 08 '12 at 08:19
-
thanks for the http property tip, was stuck before I saw this answer – jcvandan Jul 30 '12 at 09:34
-
Darin, in your first sample, after UPDATE:, you're populating a URL but then not using it. At least drop it into the ViewBag. Had me very confused of your sample's relevance for a moment. – Jon Davis Dec 03 '12 at 22:59
-
@stimpy77, the idea of my update is to show that you could generate an url to a Web API method inside a controller action and inside a view if necessary. The question here was not how to use this url but how to generate it. I don't see why I would be putting it in a ViewBag. As seen in my second example this very same url could be generated inside a view if needed. – Darin Dimitrov Dec 04 '12 at 06:43
-
2I understand the purpose of the sample; I'm saying it would be a more useful sample if you threw in one line of ViewBag code because you would be demonstrating that *this value is what we were trying to get at in a likely scenario* and not just executing some arbitrary code. Again, I wouldn't have commented if I hadn't wasted a couple minutes failing to realize what I missed before I finally recognized that the particular line of code was the relevant one. – Jon Davis Dec 04 '12 at 23:51
It works with the simpler form of Url.Action
thus you don't have to reference any Routing names:
Url.Action("ActionName", "ControllerName", new { httproute = "DefaultApi" })
You might want to add an area = ""
if the URL is needed within an Area. (Api controllers are outside of Areas by default.) I'm using MVC 4.

- 13,848
- 21
- 92
- 137

- 5,899
- 2
- 35
- 48
-
7The UrlHelper in Web Api is http://msdn.microsoft.com/en-us/library/system.web.http.routing.urlhelper(v=vs.108).aspx, it doesn't have an action method. – Yuriy Faktorovich Sep 20 '13 at 15:26
-
8@YuriyFaktorovich This post implies that `Url.Action` is being used from a View. `Url` is of type `System.Web.Mvc.UrlHelper`, and it doesn't map to `WebApi` routes. This is why you need the extra parameter: `new { httproute = "" }`. – Jesse Sep 24 '13 at 21:26
-
3
-
I had to add the link with a blank area and null htmlAttributes `@Html.ActionLink("link", "Get", "Servers", new { id = r.idServer, area = "", httproute = "" }, null) ` but that worked perfectly – Christopher G. Lewis Aug 04 '15 at 22:07
-
2This works perfectly from a view, however, I am not sure why it generates the name of the action as a query string like: /api/dashboard?action=getdata. I was expecting this: /api/dashboard/getdata – umutesen Nov 08 '16 at 07:47
Want to be able to generate links in a typesafe manner, without hardcoded strings (controller names)?
There's a nuget for that! (and it's written by Mark Seeman)
https://github.com/ploeh/Hyprlinkr
Works like this:
Routes, as usual:
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
Get an URL:
var linker = new RouteLinker(request);
var uri = linker.GetUri<FooController>(r => r.GetById(1337));
Result:
http://localhost/api/foo/1337

- 34,633
- 32
- 143
- 233
Here is the KISS method for answering the question:
If this is the code that you would use to create a MVC controller URL
@Url.Action("Edit", "MyController")
In order to get a URL for the API version of the controller (assuming you use the same controller name) you can use
@Url.Action("Edit", "api/MyController")
All the Url.Action method is doing is appending the root path of the application, with the controller name, followed by the action name (unless it is "Index" in which case it is not appended. if the route values object has an id property the value is also appended to the URL.

- 59
- 1
- 2
-
Thank you! I was missing the `api/` before my controller name. – Daniel Congrove Jun 27 '19 at 13:35