Ian Warburton
Ian Warburton

Reputation: 15676

How to generate URL from WebAPI method

Here's my WebAPI action..

public class AuthenticationController : ApiController
{

    [Route("api/auth/login")]
    [HttpPost]
    public object Login([FromBody] LoginViewModel loginViewModel)

    ...

How do I generate a URL inside a MVC view?

I've tried this...

@Url.HttpRouteUrl("DefaultApi", new { controller = "Authentication", action=  "Login" })"

... but it says that DefaultApi doesn't exist.

Why does the method want a route name and route parameters?

How did they manage to make routing even more complicated than regular MVC?

What kind of web framework makes requesting a web page / API method / web service thing difficult? Its ridiculous.

Upvotes: 1

Views: 662

Answers (1)

Julito Avellaneda
Julito Avellaneda

Reputation: 2385

the problem is because you are using attibute routing, so you need to declare the name of the route with the attribute Route, like:

[Route("api/auth/login", Name = "RouteName")]

And when you use @Url.HttpRouteUrl:

@Url.HttpRouteUrl("RouteName", new { controller = "Authentication", action=  "Login" })"

Upvotes: 1

Related Questions