Simple route: No HTTP resource was found that matches the request URI - asp.net-web-api

I have 2 simple routes in my WebApiConfig:
config.Routes.MapHttpRoute(
name: "Speaker",
routeTemplate: "api/presentations/{presentationid}/speakers/{id}",
defaults: new { controller = "speakers", id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "Presentations",
routeTemplate: "api/presentations/{id}",
defaults: new { controller = "presentations", id = RouteParameter.Optional }
);
I can't get the first route to work.. I have a SpeakersController with a Get:
IEnumerable<SpeakerModel> Get(int presentationid)
{
return _speakerService.GetSpeakersByPresentationId(presentationid).Select(s => _modelFactory.Create(s));
}
And when I call : http://localhost/Api/Presentations/1/Speakers via a get request I get this error :
"Message": "No HTTP resource was found that matches the request URI 'http://localhost/Api/Presentations/1/Speakers'.",
"MessageDetail": "No action was found on the controller 'Speakers' that matches the request."

I did not have a public access modifier on my method.
**public** IEnumerable<SpeakerModel> Get(int presentationid)
{
return _speakerService.GetSpeakersByPresentationId(presentationid).Select(s => _modelFactory.Create(s));
}

Related

custom http route for an API POST without URI parameter

I have a post http without header URI parameters, in the image you can see API
When i call it I get the error
No HTTP resource was found that matches the request URI
I'm trying to add a custom route as this in the config file
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}",
defaults: null,
constraints: new { action = "accettaTickets" }
);
because the default needs a URI parameter
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
but I get the same error.
Can you help me?
Thanks!
I suppose you don't have controller with proper action method for this URL. Try to add such controller with default route configuration.
public class WATicketController : ApiController
{
public IEnumerable<string> accettaTickets()
{
return new string[] { "value1", "value2" };
}
}

How do I set Route attribute in this scenario?

In MVC WEB API using C# and .Net framework 4.5.1 I have a controller name MonitoringController as bellow:
public class MonitoringController : ApiController
{
[HttpGet]
[ActionName("list")]
public IEnumerable<string> List(string collection)
{
return new String[] { "test 1", "test 2" };
}
}
and my routing config is like this:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.MapHttpAttributeRoutes();
config.EnsureInitialized();
The api works fine on get requests e.g. http://localhost/api/monitoring/list?collection=test
How do I apply Route attribute to make sure it works on http://localhost/api/monitoring/channels/list?collection=test
What I thought I should do was :
[RoutePrefix("api/monitoring")]
public class MonitoringController : ApiController
{
[HttpGet]
[Route("channels")]
[ActionName("list")]
public IEnumerable<string> List(string collection)
{
return new String[] { "test 1", "test 2" };
}
}
I cannot get http://localhost/api/monitoring/channels/list?collection=test working! What have I done wrong?
I want to be able to have the following routes defined in the controller:
/api/monitoring/channels/list
/api/monitoring/windows/list
/api/monitoring/doors/list
Thanks for your help
Try flipping the order in which you register the routes:
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
And also change [Route("channels")] to [Route("channels/list")] and dump the ActionName attribute.
Better still, don't mix the two approaches and go attribute routing throughout.
If you want the collection to be part of the route use a route parameter.
[Route("{collection}/list")]
See http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

something wrong with wep api route

My registered routes mapping looks like this:
config.Routes.MapHttpRoute(
name: "WithActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
And, controller looks like this:
// [Route("???????????")]
[HttpGet]
public HttpResponseMessage GetPatientProviderData([FromUri] PatientProviderIncomingDto ppDto)
{
var response = Request.CreateResponse(HttpStatusCode.OK, _claimDetailService.GetPatientProviderData(ppDto.patientId, ppDto.facilityGroupId, ppDto.claimId));
return response;
}
I'm making a GET call like this:
http://localhost:xxxx/api/claims/GetPatientProviderData?patientId=180&facilityGroupId=9&claimId=21
But, it's not hitting the URL. I was expecting it to hit the URL and that the supplied parameters get converted into DTO.

How do i redirect when version number is not included in url (Url Web Api Versioning)

i have this route attribute in my api controller "[RoutePrefix("api/v5/test")]". I want to allow my api to be consumed without specifying the version no. If no version no is included in url, then server will redirect request to latest api version. How can i do this?
/api/v1/products
config.Routes.MapHttpRoute(
name: "V1Api",
routeTemplate: "api/v1/products",
defaults: new { controller = V1_Products }
);
/api/v2/products
config.Routes.MapHttpRoute(
name: "V2Api",
routeTemplate: "api/v2/products",
defaults: new { controller = V2_Products }
);
/api/products
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/products",
defaults: new { controller = V2_Products }
);
There is no way to add multiple RoutePrefix's on a controller.
One possible solution is to add multiple routes for each method. You can use '~' to specify the absolute path and ignore the RoutePrefix for the controller.
Example:
[RoutePrefix("api/v5/test)]
public class TestV5Controller : ApiController
{
[Route("{id}")]
[Route("~/api/latest/test/{id}")]
[HttpGet]
public IHttpResult Get()
{
return Ok("Hello World");
}
}

how to route this in mvc web api?

I have this requirement to map the url
api/v1/Contact/12/tags
in the above url the Contact is the controller, and 12 is the contactId and tags is the action is want to be called.
The method is like this
[HttpGet]
public List<TagModel> Tags([FromUri]int contactId)
I have did like this
config.Routes.MapHttpRoute(
name: "RouteForHandlingTags",
routeTemplate: "api/v1/{controller}/{id}/{action}",
defaults: new { },
constraints: new { id = #"\d+" }
);
I am getting the error "No HTTP resource was found that matches the request URI http://localhost:4837/api/v1/contact/12/tags"
I have tried
[HttpGet]
public List<TagModel> Tags(int contactId)
and
config.Routes.MapHttpRoute(
name: "RouteForHandlingTags",
routeTemplate: "api/v1/{controller}/{id}/{action}"
);
But all are giving the same error. But when I do a call like this
api/v1/Contact/12/tags?contactid=12
everything works fine. But this is not what I want. The contactId is specified after contact. Can anyone please tell me how to route this?
The parameter names in your action and route must match.
You can either change your action to:
[HttpGet]
public List<TagModel> Tags(int id)
Or change your route to:
config.Routes.MapHttpRoute(
name: "RouteForHandlingTags",
routeTemplate: "api/v1/{controller}/{contactId}/{action}",
defaults: new { },
constraints: new { id = #"\d+" }
);
Note: you do not need [FromUri] with a primitive type like an int.

Resources