Get URL for route prefix in ASP.NET WEB API in Razor mode

I have an API

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

And all is well ... except that I would like to pass the URL of this route to some JS so that it can create URLs for the request Things

So in razor mode I tried

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

but i get an empty string

I tried to give the controller an explicit name route and using

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

ut, as an empty string.

Am I really stupid? Should this work?


Update after more complicated actions.

On the MVC controller (where I need to know the route of the API controller)

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

All of them are equal to zero.

If I delete the condition area="", then I get the route, but with the name Area in it, and the route does not have the name of the region in it.

?

+4
1

TL; DR - -

, , - .

- , , MVC API .

:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
        routes.MapRoute(
            name: "Default",
            url: "home",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new [] { "Another.Project.Controllers" }
        );
    }

WAT?

:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

, .

, , ( ), , .

: " - ?"

+3

All Articles