我在向我的web api传递参数时很挣扎。如何通过angularjs在Web api中传递多种类型的参数?
有两种情况我会列出他们两个。
我想通过以下
[HttpGet] public string VerifyName(string name) { return name + "hi"; }
一个简单的字符串PARAM这样对于那些我在我的angularjs控制器创建这样一个URL。
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;
});
这使返回404还
{"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."}
- 同样,当我试图传递一个对象时,它被赋予了相同的结果
我的角度功能
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;
});
网络API方法
[HttpGet]
public string VerifyLogin(UserLoginDomainModel loginModel)
{
//do some business logic
return "abc ";
}
响应是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 }
);
}
我想有一些问题,这是基本的路由,但一些如何不能弄清楚它是什么,请建议
最好将POST复杂对象发布到API – dbugger
首先注意到你没有提到你的路径的基础url完整url它应该包括'localhost'以及 –
http://stackoverflow.com/questions/19049989/web-api- get-method-with-complex-object-as-parameter – dbugger