I am struggling pretty badly in passing parameters to my web api.
There are two case I will list them both.
I want to pass a simple string param like this below
[HttpGet] public string VerifyName(string name) { return name + "hi"; }
For which I am creating a url like this in my angularjs controller.
var name = "hello";
var msg = "";
$http.get('/api/VisitorWeb/VerifyName', name).success(function (data) {
msg = data;
}).error(function (data) {
$scope.error = "An error has occured while adding! " + data;
});
This keeps returning 404. Also
{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:43516/api/VisitorWeb/VerifyName'.","MessageDetail":"No action was found on the controller 'VisitorWeb' that matches the request."}
- Similarly when I am trying to pass a object it is giving the same result
My angular function
var loginModel = {
UserName: $scope.UserName,
PassWord: $scope.Password
};
var msg = "";
$http.get('/api/VisitorWeb/VerifyLogin', loginModel).success(function (data) {
msg = data;
}).error(function (data) {
$scope.error = "An error has occured while adding! " + data;
});
Web Api Method
[HttpGet]
public string VerifyLogin(UserLoginDomainModel loginModel)
{
//do some business logic
return "abc ";
}
Response is 404.
WebApiConfig
public static void Register(HttpConfiguration config)
{
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
I am thinking there is some problem with the routing that is basic but some how not able to figure out what it is , Please suggest
localhostas wellidparameter instead ofname, you could then modify your angular call to$http.get('/api/VisitorWeb/VerifyName/' + name)and it should work. As others have pointed out, get does not accept objects.