How to create ASP.NET Web API Url?

asp.net Mvc-4asp.net Web-Api

asp.net Mvc-4 Problem Overview


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

asp.net Mvc-4 Solutions


Solution 1 - asp.net Mvc-4

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 }
);

Solution 2 - asp.net Mvc-4

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.

Solution 3 - asp.net Mvc-4

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

Solution 4 - asp.net Mvc-4

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShane CourtrilleView Question on Stackoverflow
Solution 1 - asp.net Mvc-4Darin DimitrovView Answer on Stackoverflow
Solution 2 - asp.net Mvc-4Gábor ImreView Answer on Stackoverflow
Solution 3 - asp.net Mvc-4Cristian DiaconescuView Answer on Stackoverflow
Solution 4 - asp.net Mvc-4James BunchView Answer on Stackoverflow