Thilok Gunawardena
Thilok Gunawardena

Reputation: 924

How to do the proper routing in ASP.Net WebAPI

I am developing an application using ASP.Net Web API and to configure application routing, I'm following this method. But in my application, the url i want is the below. "http://localhost/MY/api/user/AllGridData/false/1342006446883/10/1/Id/desc"

I manually typed this url in my browser window and AllGridData method is fired. But when I access it from the application, it tries to access the url like below. "http://localhost/MY/api/user/AllGridData?_search=false&nd=1342006446883&rows=10&page=1&sidx=Id&sord=desc"

So then the AllGridData method is not fired because of the following exception. Failed to load resource: the server responded with a status of 400 (Bad Request) "http://localhost/MY/api/user/AllGridData?_search=false&nd=1342006446883&rows=10&page=1&sidx=Id&sord=desc"

My routing for this url in RouterConfig.cs is like this.

routes.MapHttpRoute(
            name: "LoadGridApi",
            routeTemplate: "api/{controller}/{action}/{_search}/{nd}/{rows}/{page}/{sidx}/{sord}",
            defaults: new { _search = UrlParameter.Optional, nd = UrlParameter.Optional, rows = UrlParameter.Optional, page = UrlParameter.Optional, sidx = UrlParameter.Optional, sord = UrlParameter.Optional }
        );

Hope you understand the problem. Ultimately what I want is to get the URL like below, so then it AllGridData method is fired. "http://localhost/MY/api/user/AllGridData/false/1342006446883/10/1/Id/desc"

What am I doing wrong? Is it because of wrong routing code?

My AllGridData method is in a WebAPI controller, not in a MVC controller.

Upvotes: 1

Views: 767

Answers (2)

eatfrog
eatfrog

Reputation: 95

You can use actions just fine in WebApi too. You just don't need them if you are trying to build something RESTful.

example:

public HttpResponseMessage OpenBill(long billId)

Gets it route from:

routes.MapHttpRoute(
 name: "DefaultApiWithActionAndId",
 routeTemplate: "api/{controller}/{action}/{id}",
 defaults: new { id = RouteParameter.Optional }
  );

Also, you do not have to specifiy querystrings with more than one route. This is why /api/organisations/2/3 works and /2?nd=3 works.

IMHO you should try to keep the routes as simple as possible, otherwise you are in for a world of hurt if something stops working and you have to debug your routes. So i'd go with the querystring version.

Why you are getting urls that are a querystring I cannot answer. It depends who is making the url. This is how GETs usually are done.

Upvotes: 1

stevethethread
stevethethread

Reputation: 2524

thilok. I just tried something similar, with a simpler routing.

routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}/{nd}",
                defaults: new {id = RouteParameter.Optional, nd = RouteParameter.Optional}
                );

and used jQuery getJson calls to call the following method.

public IEnumerable<Organisation> Get(int id, int nd)
        {                        
            using (_unitOfWork)
            {

I found that I was able to make calls using all the following Urls

/api/organisations/2/3, /api/organisations/2?nd = 3 and /api/userorganisations/?id=2&nd=3

This also worked when directly calling them from a browser window. (Chrome).

What I have notice that you have wrong is that you have an extra placeholder for action {action}. This should not be there for WebApi routing. This is for MVC routing only. There is no concept of action for WebApi's, as this is all handled by the HttpVerbs.

Further information can be found here

WebApi routing

Upvotes: 2

Related Questions