1
votes

I'm new to c# and asp.net web api.
I'm working with Microsoft Visual Studios 2015 Community Edition.
A few days back i got an old Code. It used the old NuGet-Packages of Microsoft.AspNet.WebAPi(Client/Core/WebHost)(~v4.2).
Then i installed/updated the new Versions (~v5.2.3).
I did that in order to use the Attribute Routing of Web API v2.
I changed some stuff in order to accommodate the version changes. How to Upgrade an ASP.NET Project
I did most of the Steps described in the Link. I didn't do the Steps to change the config Files in the View Folder, because this project doesn't have a View.
The I added 1 simple Methode with Route Attribute and added Route Attribute to an existing Methode

Sample 1:

[HttpGet]
[Route("Hello/world/{num:int}")]
public IHttpActionResult GetSimple(int num)
{
    return Ok(num);
}

Sample 2:

[HttpGet]
//[AktionName("Confidential Stuff")]
[Route("Hello/world/stuff/{Id:int}/{Id2:int}")]
public HttpResponse Message GetConfidentialStuff(int Id, int Id2)
{
    //do some stuff
    //return stuff
}

Build the Project --> Worked ( Warnings about stuff beeing obsolete, but thats a small matter )
Run the Project --> Worked
Used Postman to perfrom the Request. --> partially worked
It did work with calling ' localhost/Hello/world/123 'and returned the 123.
But
It didnt work with calling ' localhost/Hello/world/Stuff/1/2. It returned: HTTP resource was found that matches the request URI (link to adress)

Edit

Well now here are some of my questions:
* What is the Problem ?
My URI i used in Postman was wrong.(misspelled & wrong parameters)
* How do i fix it?
Use right URI and parameters
* Why does Sample 1 work but Sample 2 doesn't?
Because i was stupid & didn't check
* If the Code is messed up, why does it compile? Should it not throw an Error?
Still don't know specific answer, but yes it should throw an Error if code is wrong
* If there are problems with the code used in Sample 2, should it not still find the adress/resource but then return false Information
Yes, it should

1

1 Answers

1
votes

The problem is that the convention routing is not setup to match the routing defined in Sample 2. The default convention routing is only supporting one extra param like api/{controller}/{id} which matches Sample 1s method definition.

To solve the problem:

Either you edit the convention in the WebApiConfig.cs class to accept an extra optional routing parameter like this

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}/{id2}",
    defaults: new { id = RouteParameter.Optional, id2 = RouteParameter.Optional,  }

);

OR, you take advantage of Attribute Routing by defining the props in the querystring instead.

Define the method like this

[Route("Hello/world/stuff")]
public HttpResponse Message GetConfidentialStuff(int Id, int Id2)
{
    //do some stuff
    //return stuff
}

and call it with Hello/world/stuff?id=1&id2=2

Remeber to enable the Attribute Routing in the WebApiConfig.cs class

config.MapHttpAttributeRoutes();