I have MVC project, I have added one API controller.in this API controller I have created methods in it.but when I am trying to call this API from postman or localhost with "http://localhost:10133/api/BedfordBrownstoneApi/GetAgentId?username=dgsdgsdgsd&password=sdgsdgs" Url its gives following response.
{
"Message": "No HTTP resource was found that matches the request URI 'http://localhost:10133/api/BedfordBrownstoneApi/GetAgentId?username=dgsdgsdgsd&password=sdgsdgs'.",
"MessageDetail": "No type was found that matches the controller named 'BedfordBrownstoneApi'."
}
My API controller is like following.
public class BedfordBrownstoneApi : ApiController
{
// GET api/<controller>
public int GetAgentId(string username,string password)
{
DataContext db = new DataContext();
var data = db.Set<AgentLogin>().Where(a => a.UserName==username && a.Password==password).SingleOrDefault();
return data.AgentId;
}
}
}
My WebApiConfig class is like following.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}",
defaults: new { action = "GetAgentId" }
);
}
}
Check Below Question that that similar problem as yours.
Check Accepted Answer of 'Benoit74B'. It clearly explains problem in details. Actual problem is regarding parameters you are passing -
"No HTTP resource was found that matches the request URI" here?
Remove "API" from controller name BedfordBrownstoneApi.
Change it from
http://localhost:10133/api/BedfordBrownstoneApi/GetAgentId?username=dgsdgsdgsd&password=sdgsdgs
to
http://localhost:10133/api/BedfordBrownstone/GetAgentId?username=dgsdgsdgsd&password=sdgsdgs
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { action = "GetAgentId",id = RouteParameter.Optional }
);
add Id parameter in your routeTemplete in webapiconfig.
Have you enabled attribute routing in api config? config.MapHttpAttributeRoutes();
With your current route, you should hit it via query string "http://localhost:10133/api/BedfordBrownstoneApi/GetAgentId?username=dgsdgsdgsd&password=sdgsdgs" and decorate the parameters with [FromUri]
[HttpGet]
[Route("api/BedfordBrownstoneApi/GetAgentId/")]
public int GetAgentId(string username,string password)
{
//api stuff
}
or to hit the api via route parameters - http://localhost:10133/api/BedfordBrownstoneApi/GetAgentId/yourusername/yourpassword
[HttpGet]
[Route("api/BedfordBrownstoneApi/GetAgentId/{username}/{password}")]
public int GetAgentId(string username,string password)
{
//api stuff
}
Hope this helps you. Thanks!!
There are two issues.
Controller name - Change the name, add Controller in the name (BedfordBrownstoneApiController).
You missed calling 'config.MapHttpAttributeRoutes()' in Register function.
Please update your 'Register' function in 'WebApiConfig' as below.
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}",
defaults: new { action = "GetAgentId" }
);
}
I have just change my code to following and now its work.
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "BedfordBrownstoneApi",
routeTemplate: "api/{controller}/{action}",
defaults: new { action = "GetAgentId" }
);
}
Related
I am make POST call into my Web API using Postman. I get the error: "The requested resource does not support http method 'GET'."
I am not making a GET call. If I do call one of my GET methods, it works fine and returns the expected result.
My controller class:
[RoutePrefix("api/login")]
public class LoginController : ApiController
{
ModelContext db = new ModelContext();
[HttpPost]
[Route("validate")]
public HttpResponseMessage Validate([FromBody] LoginViewModel login)
{
try
{
var message = Request.CreateResponse(HttpStatusCode.OK);
return message;
}
catch (Exception ex)
{
var message = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
return message;
}
}
}
I have the Web API running locally and call with this URL:
http://localhost:44303/api/login/validate
This url returns:
<Error>
<Message>
The requested resource does not support http method 'GET'.
</Message>
</Error>
My routing in WebApiConfig.cs
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
While this controller message returns HttpResponseMessage, I have tested by changing the response to "string" and just returning a string value, but I get the same error.
I have read through so many SO posts but none appear to fix my issue. I am at a total loss to explain this behavior. I appreciate any ideas.
EDIT
I have tested GETs in other controllers and they are returning data as expected.
EDIT FOR CONTEXT 6/3/2020
This method in the default ValuesController works:
[Route("api/values")]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
These 2 methods in the SAME controller do not work:
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
EDIT #2 6/3/2020
Now all of my api methods are working for the default ValuesController. I do not know why.
My custom POST methods in other controllers such as the Post method above are still not working. Here is my current WebApiConfig.cs:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "Api_Get",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional, action = "Get" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);
config.Routes.MapHttpRoute(
name: "Api_Post",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional, action = "Post" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }
);
}
I do not have any middleware or special routing that I know of. Any exception handling would be simple try-catch.
I am trying to use attribute routing vs convention but that seems to be an issue.
You don't need to create a route table by HttpMethods because you have [HttpPost] attribute.
Replace all config.Routes.MapHttpRoute by
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
It's preferable to use Attribute Routing for Web API project. It will reduce the chances of errors because in RouteConfig class there can be any mistake while creating a new custom route. Also, you don’t have to take care of the routing flow i.e, from most specific to most general. All here is the attribute you use the action method.
I am new to learning Web API code in C# and was wondering how to create the following link that gets the name for a specific value ID.
The provided code below does not work as I want it to with the "/name" behind it.
// GET api/values/{id}/name
public string Get(int id)
{
return getNameValue(id);
}
You can use custom route like:
[Route("{id:int}/name")]
public string Get(int id)
{
return getNameValue(id);
}
Update:
In WebApiConfig, modify register method to have custom routes
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id:int}/name",
defaults: new { id = RouteParameter.Optional }
);
You can directly use attribute routing without defining the route in route config.
Add the below code which enable attribute routing(This feature is only applicable to Web API 2).
Attribute routing Web API 2
public static void Register(HttpConfiguration config)
{
// Attribute routing.
config.MapHttpAttributeRoutes(); //**enabling attribute routing here.**
// Convention-based routing.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
then you can make use of:
[Route("{id:int}/name")]
public string Get(int id)
{
return getNameValue(id);
}
I've got a simple web API that registers on one route. At the moment I've got two because only one of them does what I need.
My application only has one controller and one Post method in that Controller. I've registered a single Route which always returns a 405 (method not allowed)
The two routes are configured in the RouteConfig.cs:
routes.MapHttpRoute(
name: "app-events",
routeTemplate: "events",
defaults: new { controller = "Events" },
handler: new GZipToJsonHandler(GlobalConfiguration.Configuration),
constraints: null
);
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
The Controller method is essentially this...
public class EventsController : ApiController
{
public EventsController()
{
_sender = new EventHubSender();
}
public async Task<HttpResponseMessage> Post(HttpRequestMessage requestMessage)
{
// doing fun stuff here…
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
If I only configure the first route and post a request to http://devbox/events I will get a 405. However, if I add the second, default, route, and post to http://devbox/api/events I get back my expected 201.
With both routes configured at the same time, the same pattern, the route explicitly bound to the Controller receives a post request and fails with a 405, but the other URL will work.
I've spent a long time looking around before conceding to ask the question. Most things I read have a lot to do with Webdav and I think I've followed every one of them to fix the issue. I am not very experienced with this stack, so nothing is very obvious to me.
You mentioned RouteConfig File. This is used for configuring the MVC routes not Web API routes.
So it would appear you are configuring the wrong file which would explain why the api/... path works as it is probably mapping to the default configuration in WebApiConfig.Register, which would look like
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
// Convention-based routing.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
You would need to update that file with the other desired route
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
// Convention-based routing.
config.Routes.MapHttpRoute(
name: "app-events",
routeTemplate: "events",
defaults: new { controller = "Events" },
handler: new GZipToJsonHandler(GlobalConfiguration.Configuration),
constraints: null
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Web API routes are usually registered before MVC routes which explains why it was not working with your original configuration.
You should also adorn the action with the respective Http{Verb} attribute.
In this case HttpPost so that the route table knows how to handle POST requests that match the route template.
[HttpPost]
public async Task<IHttpActionResult> Post() {
var requestMessage = this.Request;
// async doing fun stuff here….
return OK();
}
I am trying understand Web Api 2 (MVC project in Visual Studio).
The method is
[HttpPost]
public string Post(int id, string e, bool o)
///code removed
Using Postman, I can query using Post and the path http://localhost:62093/api/Demo/5. This works and returns the expected value.
Now I want to add more parameters and this is where it goes wrong!
I have updated my method to
[HttpPost]
public string Post(int id, string e, bool o)
Now when I attempt to query this using (again) Post and the path http://localhost:62093/api/Demo/5 I see
"Message": "The requested resource does not support http method 'POST'."
I then try to change the URL, so when I use Post and the new path http://localhost:62093/api/Demo/5/a/false I see an HTML file response of
The resource cannot be found
This has been mentioned before on Stackoverflow and from what I've understood is about the URL being 'incorrect'
Thinking this could be an issue with routes I updated mine to
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}/{s}/{o}",
defaults: new { id = RouteParameter.Optional, s = RouteParameter.Optional, o = RouteParameter.Optional }
);
But the same issue persists. I'm not sure what I've done wrong.
This is a routing issue. you have not configured your routes correctly.
First let us update the WebApiConfig.cs file
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
// Attribute routing.
config.MapHttpAttributeRoutes();
// Convention-based routing.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Now with that configured I would suggest using Attribute routing
public class DemoController : ApiController {
[HttpPost]
[Route("api/Demo/{id:int}/{e}/{o:bool}")] //Matches POST api/Demo/5/a/false
public IHttpActionResult Post(int id, string e, bool o) {
return Ok();
}
}
I would finally suggest reading up on Attribute Routing in ASP.NET Web API 2 to get a better understanding on how to properly route to your API controllers
I have a problem that I want to call a MVC Api method with a custom name.
I changed the WebApi.config as described here
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id="test" }
);
and wrote a class
public class MissingCardBoxModelController : ApiController
{
// GET api/missingcardboxmodel
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/missingcardboxmodel/5
public string Get(string id)
{
return id;
}
public string GetTrackingNumber(string parcelLabelNumber)
{
string trackingNumber = "some number";
return trackingNumber;
}
// POST api/missingcardboxmodel
public void Post([FromBody]string value)
{
}
// PUT api/missingcardboxmodel/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/missingcardboxmodel/5
public void Delete(int id)
{
}
}
But I can't call the method via http://localhost:58528/api/MissingCardBoxModel/GetTrackingNumber/123456
I Get the message
No action was found on the controller 'MissingCardBoxModel' that
matches the request.
Why can't I call the method ?
If your routes are configured to be these (default in the MVC solution template):
url: "{controller}/{action}/{id}"
You should change parcelLabelNumber to id.
You can read more about routes here.
By Default Web API allows Restful conventions that means it will auto map GET, PUT, POST, DELETE etc action names. if you look inside your WebApiConfig in routes it only allows the route below
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
which means it only allows
.../api/yourcontrollername/a parameter that will map to id
.
you basically have 2 options, one to use attribute routing. or you can add a route to your custom method eg:
config.Routes.MapHttpRoute(
name: "custom",
routeTemplate: "api/{controller}/{action}/{parcelLabelNumber}",
defaults: new { parcelLabelNumber = "" }
);
please also notice the parameter name here "parcelLabelNumber", you have to name your parameter same here as in your action. You should be able to reach this action at - http://localhost:23691/api/MissingCardBoxModel/GetTrackingNumber/1245
Also please have a look at Routing in general