I have a Web API 2 API Controller

public class ThingsController : APIController
  [Route("{thingId}", Name="Things.Get")]
  public Thing Get(string thingId)

And all is well... except I'd like to pass the URL for that route into some JS so that it can construct URLs to request Things

So in the razor view I've tried

myJavascriptObject.thingUrl = '@Url.Action("Get", "Things", new {area=""})';

but I get a blank string

I've tried giving the controller route an explicit name and using

@Url.RouteUrl("Things.Get", new {areas="", thingId="foo"}") b

ut that's an empty string too.

Am I being silly? Should this work?

Update after more trying of things.

On an MVC controller (Where I need to know the API Controller route)

var something = Url.Action("Get", "Things", new {area = ""});
var somethingWithId = Url.Action("Get", "Things", new { area = "", siteId = "fakeId" });
var somethingElse = Url.RouteUrl(new {Action = "Get", Controller = "Things", Area = ""});
var somethingElseWithId = Url.RouteUrl(new { Action = "Get", Controller = "Things", Area = "", siteId = "fakeId" });

These are all null.

If I remove the area="" condition then I get a route but with the Area name in it and the route doesn't have the area name in it.


Related posts

Recent Viewed