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
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 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" }
);
}
Hi Guys i am new with web api routes and i have this issue where my call will pick up the more generic one over the specific one.
The ajax call i have is
$.getJSON("/api/solutions/GetSolutionByCategory/" + categoryId,
function (data) {//..some other functions}
Within the solutions controller there are 2 methods
[HttpGet]
public IHttpActionResult GetSolutionByCategory(int cateogryId)
{
List<Solution> solutions = _context.Solutions.Where(s => s.CategoryId == cateogryId).ToList();
return Ok(solutions.Select(Mapper.Map<Solution, SolutionDto>));
}
[HttpGet]
public IHttpActionResult GetSolutions()
{
return Ok(_context.Solutions.ToList().Select(Mapper.Map<Solution, SolutionDto>));
}
And then i have the following 3 routes
config.Routes.MapHttpRoute(
name: "WithAction",
routeTemplate: "api/{controller}/GetIssuesByFlag/{flag}",
defaults: new {flag = 3}
);
config.Routes.MapHttpRoute(
name: "SolutionByCategory",
routeTemplate: "api/{controller}/GetSolutionByCategory/{categoryId}",
defaults: new {categoryId = -1}
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new {id = RouteParameter.Optional}
);
What happens is that my ajax call will ignore the 2nd one that is the one i want it to hit and goes to the 3rd one there for instead of calling the GetSolutionsByCategory it hits the generic GetSolutions
What am i doing wrong here?
There is a typo in your action parameter name, its int cateogryId instead of int categoryId - public IHttpActionResult GetSolutionByCategory(int categoryId).
However, I would suggest you to go for attribute routing instead of adding lots of route configurations. Enable attribute routing in your web api config class - config.MapHttpAttributeRoutes(); and in your controller:
[RoutePrefix("api")]
public class SolutionsController:ApiController
{
[HttpGet]
[Route("GetSolutionByCategory/{categoryId})"]
public IHttpActionResult GetSolutionByCategory(int categoryId)
{
....
}
[HttpGet]
[Route("GetSolutions")]
public IHttpActionResult GetSolutions()
{
...
}
}
Using Attribute routing we can have same controller with multiple get and post methods. We need to add the routing on the action methods.
We can provide the constraints as well with attribute routing.
I have the following example where the request is http://{domain}/api/foo/{username} but I get a 404 status code back. No other Get actions exist on this controller. Shouldn't this work?
public class FooController : ApiController
{
public Foo Get(string username)
{
return _service.Get<Foo>(username);
}
}
By default your route will look something like this:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
When you visit the url http://{domain}/api/foo/{username} the controller is mapped as foo and the optional id parameter is mapped to {username}. As you don't have a Get action method with a parameter called id a 404 is returned.
To fix this you can either call the API method by changing the URL to be explicit about the parameter name:
http://{domain}/api/foo?username={username}
Or you could change your parameter name in your action method:
public Foo Get(string id)
{
var foo = _service.Get<Foo>(username);
return foo;
}
Or you could change your route to accept a username:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{username}",
defaults: new { username = RouteParameter.Optional }
);
On a side-project I am working on, I am creating a RESTful API using WebAPI 2.2. The thing I'm working on is a means of accessing settings for a game. An example of the kind of routes I am trying to accomplish are as follows:
http://x/api/GameSettings/ <-- Returns all settings
http://x/api/GameSettings/audio <-- Returns the 'audio' category
http://x/api/GameSettings/audio/volume <-- Returns the key 'volume' in category audio
Note: the examples are all Get requests.
I've implemented the following controller...
public class GameSettingsController : ApiController
{
// GET /api/GameSettings
public HttpResponseMessage Get()
{
// Magic
return Request.CreateResponse(HttpStatusCode.OK, model);
}
public HttpResponseMessage Get(string category)
{
// Similar.
}
public HttpResponseMessage Get(string category, string key)
{
// Slightly different, but still similar.
}
}
I bound up the following MVC routes:
// Only necessary for the main view...
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
And, I bound up the following WebAPI routes:
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ApiGeneralCommand",
routeTemplate: "api/{controller}",
defaults: new { controller = "GameSettings" }
);
config.Routes.MapHttpRoute(
name: "ApiCategoryCommands",
routeTemplate: "api/{controller}/{category})",
defaults: new { controller = "GameSettings" }
);
config.Routes.MapHttpRoute(
name: "ApiKeyCommands",
routeTemplate: "api/{controller}/{category}/{key}",
defaults: new { controller = "GameSettings", category = "master" },
constraints: new { key = "[a-z0-9.-]" }
);
...And finally, my Global.asax configuration is set up like so:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
...But one small problem.
When I navigate to http://x/api/GameSettings/audio, I get a 404 error. It's as if the category argument in the request isn't being properly associated to the Get(string category) method on my controller. This leads me to believe my routes are wrong or I'm missing something.
As a sanity check, I tested the route using a non-RESTful syntax, http://x/api/GameSettings?category=audio, which hit a breakpoint and yielded a result. This only reaffirms my theory that the WebAPI routing is off.
As an additional sanity check, I tested http://x/api/GameSettings/ and not only hit a breakpoint set in that function, but returned the expected result.
Question: What is my routing missing, that will allow http://x/api/GameSettings/audio to work like http://x/api/GameSettings?category=audio? I haven't worked with a RESTful API in a while, so I'm sure I'm missing something really dumb.
I would try to use the attribute routing. I believe that should work well for your scenario.
[RoutePrefix("api/GameSettings")]
public class GameSettingsController
{
// GET /api/GameSettings
[HttpGet]
public HttpResponseMessage Get()
{
// Magic
return Request.CreateResponse(HttpStatusCode.OK, model);
}
[Route("{category}")]
[HttpGet]
public HttpResponseMessage Get(string category)
{
// Similar.
}
[Route("{category}/{key}")]
[HttpGet]
public HttpResponseMessage Get(string category, string key)
{
// Slightly different, but still similar.
}
}
I would remove the stuff that you have added to the config.
Hope this helps.
Change the order and try.Because ASP.NET realizes that you have three routes. It will check the top-most route first and if your data can be placed in that route it will not check any more routes.
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ApiKeyCommands",
routeTemplate: "api/{controller}/{category}/{key}",
defaults: new { controller = "GameSettings", category = "master" },
constraints: new { key = "[a-z0-9.-]" }
);
config.Routes.MapHttpRoute(
name: "ApiCategoryCommands",
routeTemplate: "api/{controller}/{category})",
defaults: new { controller = "GameSettings" }
);
config.Routes.MapHttpRoute(
name: "ApiGeneralCommand",
routeTemplate: "api/{controller}",
defaults: new { controller = "GameSettings" }
);