Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create ASP.NET Web API Url?

In ASP.NET MVC, we have @Url.Action for actions. Is there something similar like @Url.Api which would route to /api/controller?

like image 902
Shane Courtrille Avatar asked Sep 25 '22 13:09

Shane Courtrille


People also ask

What is URL in Web API?

The URL API is a component of the URL standard, which defines what constitutes a valid Uniform Resource Locator and the API that accesses and manipulates URLs.

Can we use Web API with ASP NET?

Although ASP.NET Web API is packaged with ASP.NET MVC, it is easy to add Web API to a traditional ASP.NET Web Forms application. To use Web API in a Web Forms application, there are two main steps: Add a Web API controller that derives from the ApiController class. Add a route table to the Application_Start method.

How do I add a Web API to an existing ASP NET project?

In order to add a Web API Controller, you will need to Right Click the Controllers folder in the Solution Explorer and select on Add and then New Item. Now from the Add New Item window, choose the API Controller – Empty option as shown below. Then give it a suitable name and click Add.

How can we use Web API with ASP NET web form?

It's pretty simple. Go ahead, open Visual Studio 2013, go to New and choose the Project. Expand Installed > Templates > Visual C# and choose ASP.NET Web Application from the menu, give a reasonable name to your Web API project, which you want to do and finally click “OK” button.


1 Answers

The ApiController has a property called Url which is of type System.Web.Http.Routing.UrlHelper which allows you to construct urls for api controllers.

Example:

public class ValuesController : ApiController
{
    // GET /api/values
    public IEnumerable<string> Get()
    {
        // returns /api/values/123
        string url = Url.Route("DefaultApi", new { controller = "values", id = "123" });
        return new string[] { "value1", "value2" };
    }

    // GET /api/values/5
    public string Get(int id)
    {
        return "value";
    }

    ...
}

This UrlHelper doesn't exist neither in your views nor in the standard controllers.


UPDATE:

And in order to do routing outside of an ApiController you could do the following:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        string url = Url.RouteUrl(
            "DefaultApi", 
            new { httproute = "", controller = "values", id = "123" }
        );
        return View();
    }
}

or inside a view:

<script type="text/javascript">
    var url = '@Url.RouteUrl("DefaultApi", new { httproute = "", controller = "values", id = "123" })';
    $.ajax({
       url: url,
       type: 'GET',
       success: function(result) {
           // ...
       }
    });
</script>

Notice the httproute = "" route token which is important.

Obviously this assumes that your Api route is called DefaultApi in your RegisterRoutes method in Global.asax:

routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);
like image 143
Darin Dimitrov Avatar answered Oct 07 '22 16:10

Darin Dimitrov