46

How can I generate the same URL but in Web API?

var url = Url.Action("Action", "Controller", new { product = product.Id, price = price }, protocol: Request.Url.Scheme);

P.S.

The URL should be generated to an MVC controller/action but from within web API.

So basically: make a get request to my api/generateurl and that will return an URL to:

http://example.com/controller/action?product=productId&price=100
0

2 Answers 2

58

Maybe the closest helper to Url.Action in Web API Controller is the Url.Link method which will generate the URL by Route name, Controller Name, Action Name and the route parameters (if needed).

Here is a simple example

The default App_start/RouteConfig.cs

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

The Web API Controller:

public class MyWebApiController : ApiController
{
    public string Get()
    {
        var url = this.Url.Link("Default", new { Controller = "MyMvc", Action = "MyAction", param1 = 1, param2 = "somestring" });
        return url;
    }
}

The MVC Controller

public class MyMvcController : Controller
{
    public ActionResult MyAction(int param1, string param2)
    {
        // ...
    }
}

The generated URL by the WebApi controller will be http://myDomain/MyMvc/MyAction?param1=1&param2=somestring.

I didn't find how to pass the protocol/url schema but at the and it will be just a string and you can manipulate it if you know what the protocol should be.

This may help for the protocol part: Generate HTTPS link in Web API using Url.Link

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

4 Comments

what the Default means?
Default is the name of the route. See RouteConfig.cs
This doesn't appear to work if the WebAPI2 controller is decorated with a RoutePrefix [RoutePrefix("api/MyContoller")] you will get a base URL followed by controller name, instead of the desired base URL followed by the route prefix. i.e. you get: HOST/MyContoller/MyAction instead of HOST/api/MyContoller/MyAction, (Where api/MyContoller is my rout prefix - which happens to contain my controller name) suspect similar problem if the Action has a Route decoration.
I needed to get the URL of an action on the same controller, and was using the RoutePrefix & Route decorators. I found that calling Url.Content("MyRoute/"); (Where "MyRoute" was the name of the Route decorating a different Action in same controller) worked well. it gave me {HOST}/{ROUTE-PREFIX}/{ROUTE} rather than {HOST}/{CONTROLLER}/{ACTION} (which would never resolve)
2

This expands on the answer by VictorBahtev to mention ASP.NET CORE.

In 2020 there is a chance that your project is targeting ASP.NET Core 3.1. In the current version of WebAPI there is a method available to do the trick requested exactly by the OP. The method below doesn't require a route parameter (which OP didn't provide):

string url = this.Url.ActionLink("ActionName", "ControllerName", new { product = product.Id, price = price, ..}[, protocol = null, host = null, string fragment = null]);

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.