0

I have a web application that has regular webpages and an API. the regular controllers are in Controllers/, the API controllers are in Controllers/API.

On one of my views I can call

$.getJSON("/API/PRAXA/FO")

but that only works in development. In production, the application is in a subdirectory, so I would have to call

$.getJSON("/DMR/API/PRAXA/FO")

How can I determine this properly at run-time, so it works in both development and production?

I tried using

@Html.RouteLink("hiddenLink", "DefaultApi", new { action = "FO", controller = "PRAXA" }, new { id = "hiddenLink" }) 

but its href attribute is giving me null in both environments.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Adam R. Grey
  • 1,861
  • 17
  • 30
  • use UrlHelper.HttpUrlRoute with the controller, actions and parameters. the helper will generate the full url which will work for both development and production – Nkosi Oct 03 '16 at 15:53
  • @Nkosi that works perfectly, if that was an answer I'd accept it. – Adam R. Grey Oct 03 '16 at 16:02

1 Answers1

1

Use UrlHelper.HttpRouteUrl with the controller, actions and parameters. The helper will generate the full URL which will work in both development and production.

$.getJSON('@Url.HttpRouteUrl("DefaultApi", new { action = "FO", controller = "PRAXA" } )')

How do I generate a webapi url from an MVC view

Community
  • 1
  • 1
Nkosi
  • 235,767
  • 35
  • 427
  • 472