4

I have a Web API 2 API Controller

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

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.

?!

1 Answer 1

3

TL;DR - if something is weird spike it in a fresh environment

Leaving an answer here in the very remote case it helps someone else.

As somebody pointed out to me that this worked for them I created a new MVC & API project and looked at the default routing.

Ours looked like this:

    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?

I replaced it with the default route config:

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

Everything else still works and I can get the route I want.

We definitely aren't using the other namespace in this project and I'm guessing that namespaces was introduced either when this solution was part of another one (which it did use to be) or by the refactoring that moved it.

And so I've reached the natural conclusion of any really annoying bug: "How did this ever work?!"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.