I have a web API and at first I had only one controller (api/v2/game) as follows;
[System.Web.Http.RoutePrefix("api/v2/game")]
public class LoadTestController : ApiController
{
private readonly IGameServicesTest _gameServices;
#region Public Constructor
/// <summary>
/// Public constructor to initialize game service instance
/// </summary>
public LoadTestController(IGameServicesTest gameServices)
{
_gameServices = gameServices;
}
#endregion
// POST: api/Game
//[RequireHttps] For Prod Only
[ValidTimeFilter]
[ValidProductCodeFilter]
[ValidIntervalFilter]
[ValidTotalDailySale]
[ValidTerminalFilter]
[HttpPost, Route("purchase")]
public async Task<IHttpActionResult> PurchaseGame(RequestDto game){
...
Now I added one more controller (api/v2/web/game) but requests couldn't hit this new controller;
[System.Web.Http.RoutePrefix("api/v2/web/game")]
public class PalasController : ApiController
{
private readonly IGameServicesWeb _gameServices;
#region Public Constructor
/// <summary>
/// Public constructor to initialize game service instance
/// </summary>
public PalasController(IGameServicesWeb gameServices)
{
_gameServices = gameServices;
}
#endregion
[ValidProductCodeFilter]
[HttpPost, Route("purchase")]
public async Task<IHttpActionResult> PurchaseGame(RequestDto game)
{
...
Here is the webapiconfig;
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.DateTimeZoneHandling =
Newtonsoft.Json.DateTimeZoneHandling.Local;
// Web API routes
config.MapHttpAttributeRoutes();
//Registering RequestResponseHandler
config.MessageHandlers.Add(new RequestResponseHandler());
//Registering CustomExceptionFilter
config.Filters.Add(new CustomExceptionFilter());
var resolver = config.DependencyResolver;
var basicAuth = resolver.GetService(typeof(BasicAuthenticationAttribute)) as BasicAuthenticationAttribute;
// Web API configuration and services
config.Filters.Add(basicAuth);
}
I am trying with postman but requests don't reach the controller. What is missing?
Keep your friends close and your enemies even closer
There is not enough information to reproduce this issue.
I built a very basic RoutePrefix and Route test and the attributes work exactly as written in the reference documentation. Please take a few moments to debug your code.
[System.Web.Http.RoutePrefix("api/v2/web/game")]
public class PalasController : ApiController
{
// api/v2/web/game/purchase
[HttpGet, Route("purchase")]
public string PurchaseGame()
{
return "PalasController";
}
}
// api/v2/game/
[RoutePrefix("api/v2/game")]
public class LoadTestController : ApiController
{
// api/v2/game/purchase
[HttpGet, Route("purchase")]
public string PurchaseGame()
{
return "LoadTestController";
}
}
API Route configuration
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
The error message is very clear, OyunPalasController does not have a parameterless constructor. Add a parameterless constructor to OyunPalasController.
I think it is important to point out that your original post did not even mention OyunPalasController.
Has the problem you encountered at first been resolved?I have other solutions you can refer to it.
Because you have defined the default routeTemplate, when you use
RoutePrefix, it will look for a matching routeTemplate. If you don't define it, you won't be able to find it.You need to
define routeTemplate in WebApiConfig.cs.
The order in which the routes are registered in the route table is important. Register more
specific routes first, and then register more general routes to avoid false matches.
WebApiConfig.cs
config.Routes.MapHttpRoute(
name: "webgameApi",
routeTemplate: "api/v2/web/game/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "gameApi",
routeTemplate: "api/v2/game/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Best Regards,
YihuiSun
ASP.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >
Member
527 Points
2728 Posts
ASP:NET web API route attribute question
Aug 02, 2020 12:28 PM|cenk1536|LINK
Hello,
I have a web API and at first I had only one controller (api/v2/game) as follows;
Now I added one more controller (api/v2/web/game) but requests couldn't hit this new controller;
Here is the webapiconfig;
I am trying with postman but requests don't reach the controller. What is missing?
All-Star
53711 Points
24036 Posts
Re: ASP:NET web API route attribute question
Aug 02, 2020 01:07 PM|mgebhard|LINK
There is not enough information to reproduce this issue.
I built a very basic RoutePrefix and Route test and the attributes work exactly as written in the reference documentation. Please take a few moments to debug your code.
URLs
API Route configuration
Member
527 Points
2728 Posts
Re: ASP:NET web API route attribute question
Aug 02, 2020 05:19 PM|cenk1536|LINK
Hi,
Thanks but I did add this;
still no-hit to the new controller.
Member
527 Points
2728 Posts
Re: ASP:NET web API route attribute question
Aug 02, 2020 05:24 PM|cenk1536|LINK
I got this error:
All-Star
53711 Points
24036 Posts
Re: ASP:NET web API route attribute question
Aug 02, 2020 09:39 PM|mgebhard|LINK
The error message is very clear, OyunPalasController does not have a parameterless constructor. Add a parameterless constructor to OyunPalasController.
I think it is important to point out that your original post did not even mention OyunPalasController.
Member
527 Points
2728 Posts
Re: ASP:NET web API route attribute question
Aug 03, 2020 08:00 AM|cenk1536|LINK
I added this;
and it is OK.
Contributor
3040 Points
863 Posts
Re: ASP:NET web API route attribute question
Aug 03, 2020 08:54 AM|YihuiSun|LINK
Hi cenk1536,
Has the problem you encountered at first been resolved?I have other solutions you can refer to it.
WebApiConfig.cs
Best Regards,
YihuiSun