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

share|improve this question

feedback

2 Answers

up vote 18 down vote accepted

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 }
);
share|improve this answer
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 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 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 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 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 1 at 13:22
show 7 more comments
feedback

What if do not know nothing about HttpRoute Name?

EDIT: Answered by myself

RouteTable.Routes.Where(q => q is HttpWebRoute).First().GetVirtualPath(this.requestContext, new RouteValueDictionary(new { httproute = "", controller = Controller })).VirtualPath;
share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.