I'm trying to add a new Web API in a legacy web with VB.NET and .NET Framework 4.8. It has multiple Web API running correctly.
For example - CalendarController.vb:
<RoutePrefix("api/{Controller}")>
Public Class CalendarController
Inherits ApiController
<HttpGet>
<Route("{id:int}")>
Public Function GetCalendarEvent(id As Integer) As IHttpActionResult
...
Return Ok(...)
End Function
End Class
StockController.vb:
<RoutePrefix("api/{Controller}")>
Public Class StockController
Inherits ApiController
<HttpGet>
<Route("{friendlyURL}")>
Public Function GetProductStock(friendlyURL As String) As IHttpActionResult
...
Return Ok(...)
End Function
End Class
Global.asax.vb:
Sub Application_Start(sender As Object, e As EventArgs)
With RouteTable.Routes
.MapHttpRoute(name:="Calendar.GetNextCalendarEvents", routeTemplate:="api/{Controller}/Next/{maxEvents}")
.MapHttpRoute(name:="Calendar.GetMonthEntries", routeTemplate:="api/{Controller}/{month}/{year}")
.MapHttpRoute(name:="Calendar.GetCalendarEvent", routeTemplate:="api/{Controller}/{id}")
.MapHttpRoute(name:="Map.GetMarkerById", routeTemplate:="api/{Controller}/{markerId:int}")
.MapHttpRoute(name:="Map.GetAllMarkers", routeTemplate:="api/{Controller}")
.MapHttpRoute(name:="Stock.GetProductStock", routeTemplate:="api/{Controller}/{friendlyURL}")
'.MapHttpRoute(name:="Stock.GetProductStock", routeTemplate:="api/{Controller}/{friendlyURL}", defaults:=Nothing, constraints:=New With {.friendlyURL = "^[a-zA-Z]+[a-zA-Z0-9,_ -]*$"})
End With
End Sub
Testing both APIs:
const response = await fetch(`/api/Calendar/3050`);
const event = await response.json();
console.log(event);
//Returns 200 with an object correctly
{ id: 3050, title: '...', ... }
const response = await fetch(`/api/Stock/my-product`);
const product = await response.json();
console.log(product);
//Cannot access the API
GET https://localhost:44330/api/Stock/my-product 404 (Not Found)
{ Message: "No HTTP resource was found that matches the request 'https://localhost:44330/api/Stock/my-product'.",
MessageDetail: "No action was found on the controller 'Stock' that matches the request." }
const response = await fetch(`/api/Stock?friendlyURL=my-product`);
//Returns 200 Http Code and correct stock.
I cannot find out the differences.
EDIT: It runs when parameter is passed as query string
Thank you!
api/{Controller}/{Action}/{id?}and it tries to look for "my-product" action onStockController. Can you edit the question and add there every route table registration?