私はMVC4でRESTfulを使用して、WP / Android / iPhoneで使用できるAPIを取得しようとしています。そのため、初心者向けです(このメソッドは後で作り直されますが、これはテスト目的です)
サーバーと通信してjsonオブジェクトを送信し、restclientを使用してjsonオブジェクトを受信するWPアプリがあります
RestClient client = new RestClient();
client.BaseUrl = "http://*****.***/API/Account/Login";
RestRequest request = new RestRequest();
request.Method = Method.POST;
request.RequestFormat = DataFormat.Json;
User user = new User
{
UserName = "muhcow",
Password = "123456"
};
request.AddBody(user);
client.ExecuteAsync(request, response => {
System.Diagnostics.Debug.WriteLine("jo");
});
そして、最初に通常のコントローラーを作成し、次にアカウントと呼ばれるコントローラーとhttppostを取得するログインと呼ばれるアクションを作成しようとしたサーバーコードがあります。
そこで、apicontrollerから継承するコントローラーを作成しようとしましたが、LoginControllerのPostアクションにルーティングされても問題が発生しました。
したがって、投稿はDomain \ API \ Login\LoginControllerにあります
そして、ポストアクションはそのようなものです
public string Post(Login loginModel)
{
LoginResponse loginResponse = new LoginResponse();
if (Membership.ValidateUser(loginModel.UserName, loginModel.Password))
{
loginResponse.Success = true;
loginResponse.Token = "11111111111111111";
return Json.Encode(loginResponse);
}
loginResponse.Success = false;
loginResponse.Error = 1;
return Json.Encode(loginResponse);
}
しかし、ヒットされていない現在のコードで(ブレークポイントでチェック)
私はまだMapRouteを変更していないので、
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"API_default",
"API/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}