私は、Web APIにパラメータを渡すことにかなり苦労しています。anglejsからweb apiで複数のタイプのパラメータを渡すにはどうすればいいですか?
2つのケースがありますが、両方をリストします。
私は私のangularjsコントローラで、このようなURLを作成していたため
[HttpGet] public string VerifyName(string name) { return name + "hi"; }
以下、このような単純な文字列のparamを渡したいです。
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;
});
を与えているオブジェクトを渡すためにしようとしています戻って保持します
ウェブアピ方法
[HttpGet]
public string VerifyLogin(UserLoginDomainModel loginModel)
{
//do some business logic
return "abc ";
}
応答が基本であるルーティングといくつかの問題があるが、いくつかの方法がありません、それが何であるかを把握することができ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するのに最適 – dbugger
最初に、あなたのパスのベースURLのフルURLについて言及していないことに気付きました。それには 'localhost'も含める必要があります –
http://stackoverflow.com/questions/19049989/web-api-複合メソッドのオブジェクトとしてのメソッドの取得 – dbugger