Miguel Moura
Miguel Moura

Reputation: 39524

Url.RouteUrl not gettting an API route in MVC5

I created a MVC 5 site using its template and added the following API controller:

namespace MvcSite.Controllers {

  public class TestController : ApiController {

    [Route("test/send"), HttpGet]
    public String Send() {    
      return "SENT";
    }

  } // TestController
} // MvcSite.Controllers

When I access "/test/send" I get the string "SENT" back as expected ...

In a Razor view or in a Controller I need to get the URL of the send action so I tried:

var url = Url.RouteUrl(new { controller = "Test", action = "Send", HttpRoute = true });

But url is null ... I have no idea why ...

The API route is working fine so it is in the Route Table ...

What am I missing?

Thank You, Miguel

Upvotes: 2

Views: 2066

Answers (1)

Olav Nybø
Olav Nybø

Reputation: 11568

Configure a named route and use the HttpRouteUrl method to get the URL.

[Route("test/send", Name = "Send"), HttpGet]
public String Send() {    
  return "SENT";
}

Get the URL like this:

var url = Url.HttpRouteUrl("Send", new {});

Upvotes: 6

Related Questions