maproute not working / route in aspnet web api

  • 98
  • 0
  • 2018-05-30

maproute not working / route in aspnet web api 

If you're writing aspnet web api not aspner mvc and find this article.

Here is how I let routing work in aspnet web api :

1. You should know The Difference between WebApiConfig.cs and RouteConfig.cs

in aspnet web api you should write the route map in WebApiConfig.cs

 

2.see article Routing and Action Selection in ASP.NET Web API to know how to write route map

 

I choose some point to give my opinion

2-1.  in the section Extended Example:

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

in the example above that is products controller use the api/root/{id} route.

the controller =  seems like you can assign the route to which controller to use. 

 

2-2 In the article it says:


There are two special placeholders: "{controller}" and "{action}".

"{controller}" provides the name of the controller.
"{action}" provides the name of the action. In Web API, the usual convention is to omit "{action}".

in default, the {action} represent the method name in controller, 

so suppose you have routeTemplate: "{controller}/{action}/{groupId}",

and there is GetOrgAnnouncement() method in AnnounceController

then you can call this method by http://localhost:49631/Announce/GetOrgAnnouncement

 

And the route is CASE INSENESTIVE!! That's convenient.

 

3. Other routing setting I know:  [ActionName("GiveYourNameHere")] 

In the example in 2.2,  

if the original GetOrgAnnouncement() method in AnnounceController is like

[HttpGet]
public IEnumerable<Announcement> GetOrgAnnouncement()
{
   return someResult;
}

then you can add  [ActionName("GiveYourNameHere")]  to give another more suitable name to use in the {action} ,  

I give a testing example, if you use [ActionName("ThisIsATest")] ,

[HttpGet]
[ActionName("ThisIsATest")]
public IEnumerable<Announcement> GetOrgAnnouncement()
{
   return someResult;
}

then you can call this method by http://localhost:49631/Announce/ThisIsATest ,

 

3.1 Method Name starts with Get and [HttpGet]

By testing, I find that if the Method name starts with "Get", this method will be recognized as Http Get Method and don't need to add [HttpGet].

But if Method name not starts with "Get", and you want this method work as Http Get, you have to add the [HttpGet] Attribute.

 

4. Other routing setting I know:  [Route("GiveYourAbsoluteRoute")]  

if you add Route Attribute, it will make ActionName Attribute lose effectiveness,

for example, if the mehod like:

[HttpGet]
[Route("AnotherTest")]
public IEnumerable<Announcement> GetOrgAnnouncement()
{
   return someResult;
}

then you can call it by  http://localhost:49631/AnotherTest ,

the same if you use [Route("BBB/CC/D")]  , you can call by http://localhost:49631/BBB/CC/D

 

4.1 RoutePrefix

I find the example here:

https://stackoverflow.com/questions/38729352/web-api-optional-parameters-in-the-middle-with-attribute-routing

[RoutePrefix("api/Employees")]
public class CallsController : ApiController
{
    [HttpGet]
    [Route("{id:int?}/Calls/{callId:int?}")]
    public async Task<ApiResponse<object>> GetCall(int? id = null, int? callId = null)
    {
        var testRetrieve = id;
        var testRetrieve2 = callId;

        throw new NotImplementedException();
    }
}
http://localhost:61941/api/Employees/1/Calls WORKS

http://localhost:61941/api/Employees/1/Calls/1 WORKS

 

5. Http Get Parameter

Suppose the method is like

[Route("BBB/CC/D")]
public IEnumerable<Announcement> GetOrgAnnouncement(string a, string b)

then you can call it by http://localhost:49631/BBB/CC/D?a=teststring&b=teststring2

and the parameter string a, string b will pass value teststring and teststring2

 

the same if you use the mehod above you remove the Route Attribute and use [ActionName("AAA")] ,        

you can call it by  http://localhost:49631/Announce/AAA?a=teststring&b=teststring2

 

6.Http Post Simple Parameter 

if there is Post Method as

[HttpPost]
[ActionName("AAA")]
public IEnumerable<Announcement> GetOrgAnnouncement(string a, [FromBody] string b)
{
    return result
}

you can add [FromBody]   before parameter to parse the content in request body,

for example, you can call this method by PostMan ( an commonly used api testing program) like:

then string a, [FromBody] string b  will get string1, string2   

 

7.Http Post Class Parameter 

suppose there is Class Dog

public class Dog
{ 
    public string Name { get; set; }   
    public DateTime Birthday { get; set; }   
    public int Weight { get; set; }   
}

and the method:

[HttpPost]
[ActionName("AAA")]
public IEnumerable<Announcement> GetOrgAnnouncement([FromBody]List<Dog> dogs)

then you can use PostMan sending Json Format Dog Array to API:

then it'll auto parse the dogs [FromBody]List dogs  as the content that Json send.

 

8. Microsoft doc of Routing of ASPNET WEB API 

I suggest this article is must read

https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

 

---

 

Advanced info including [FromUri]  and more uage see

https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api