Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Construct url in view for Web Api with attribute routing

How can I get the url from web api in my view?

Example (from the msdn-blog):

[RoutePrefix("reviews")]
public class ReviewsController : ApiController
{
    // eg.: /reviews
    [Route]
    public IHttpActionResult Get() { ... }
    // eg.: /reviews/5
    [Route("{reviewId}")]
    public IHttpActionResult Show(int reviewId) { ... }
    // eg.: /reviews/5/edit
    [Route("{reviewId}/edit")]
    public IHttpActionResult Edit(int reviewId) { ... }
}

Now I want to construct "/reviews/edit" in my view, how can I do this?

I've tried creating a little extension method, but it requires me to give every route an actual "RouteName". Is there a method I can use (like in MVC) where I can just pass the controller and action?

@Url.Action("Edit", "Reviews)

The method I'm using now (with RouteName) also doesn't allow me to use integers as parameters (unless I pass a default value). If I do need to name all my routes, how can I create a route url, but pass my parameters in the "data"-portion of my request?

Current method:

public static string ResolveWebApiRoute(this UrlHelper urlHelper, string routeName, object routeValues = null)
{
    var newRouteValues = new RouteValueDictionary(routeValues);
    newRouteValues.Add("httproute", true);

    return urlHelper.RouteUrl(routeName, newRouteValues);
}

EDIT

When I used methods like Url.RouteUrl(new { controller = ..., action = ...}), It redirects directly to that action (e.g. new { controller = "Reviews", action = "Show"} --> /reviews/show, whilest I want it to redirect to /reviews/...

like image 796
Team-JoKi Avatar asked Jan 17 '14 12:01

Team-JoKi


People also ask

What is attribute routing in Web API?

As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API. For example, you can easily create URIs that describe hierarchies of resources. The earlier style of routing, called convention-based routing, is still fully supported.

What class can be used to generate links to routes in Web API?

In the previous section, we learned that Web API can be configured in WebApiConfig class. Here, we will learn how to configure Web API routes. Web API routing is similar to ASP.NET MVC Routing. It routes an incoming HTTP request to a particular action method on a Web API controller.

How do I use the route attribute with web API?

The Route attribute can be applied on any controller or action method. In order to use attribute routing with Web API, it must be enabled in WebApiConfig by calling config.MapHttpAttributeRoutes () method. Consider the following example of attribute routing.

What is attribute routing in ASP NET?

The ASP.NET Web API 2 and ASP.NET MVC 5 supports a new type of routing called attribute routing. As the name implies, attribute routing means attributes are used to define routes. The Attribute routing provides more control over the URIs in your Web API application by defining routes directly on the actions and controllers.

How does web API route HTTP requests to controllers?

by Mike Wasson. This article describes how ASP.NET Web API routes HTTP requests to controllers. Note. If you are familiar with ASP.NET MVC, Web API routing is very similar to MVC routing. The main difference is that Web API uses the HTTP verb, not the URI path, to select the action.

How to change the routing in webapiconfig?

Web API uses URI as “ DomainName/api/ControllerName/Id ” by default where Id is the optional parameter. If we want to change the routing globally, then we have to change routing code in register Method in WebApiConfig.cs under App_Start folder.


1 Answers

When using route attributes I was able to get the route of a WebApi2 controller from an MVC view using something like this:

Url.HttpRouteUrl("RouteName", new { })
like image 151
voam Avatar answered Sep 20 '22 03:09

voam