2017-06-13 5 views
0

web apiを使用してxamarin.androidのsql azureで新しいユーザーを作成しようとしました。私は私が得た新しいユーザーを作成するときに、私は、私はXamarin.Androidから作業していないユーザーを作成するWebApi

{"$id":"1","message":"No HTTP resource was found that matches the request URI 'http://xamari/nlogin20170612105003.azurewebsites.net/api/Login'.","messageDetail":"No action was found 

新しいユーザーを作成しようとするたびにそう、このウェブAPIが認証を行い、新規ユーザーを作成するために提供され、私のログインがエラー罰金ではありません。このエラーメッセージが表示されましたが、そのエラーメッセージ。

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Net; 
    using System.Net.Http; 
    using System.Web.Http; 
    using XamarinLogin.Models; 
    namespace XamarinLogin.Controllers 
    [RoutePrefix("api/Login")] 
    { 
     public class LoginController: ApiController 
     { 
      xamarinloginEntities db = new xamarinloginEntities(); 
      / 
      [HttpPost] 
      [ActionName("XAMARIN_REG")] 
      // POST: api/Login 
      public HttpResponseMessage Xamarin_reg(string username, string password) 
      { 
       Login login = new Login(); 
       login.Username = username; 
       login.Password = password; 
       db.Logins.Add(login); 
       db.SaveChanges(); 
       return Request.CreateResponse(HttpStatusCode.Accepted, "Successfully Created"); 
      } 
      [HttpGet] 
      [ActionName("XAMARIN_Login")] 
      // GET: api/Login/5 
      public HttpResponseMessage Xamarin_login(string username, string password) 
      { 
       var user = db.Logins.Where(x => x.Username == username && x.Password == password).FirstOrDefault(); 
       if (user == null) 
       { 
        return Request.CreateResponse(HttpStatusCode.Unauthorized, "Please Enter valid UserName and Password"); 
       } 
       else 
       { 
        return Request.CreateResponse(HttpStatusCode.Accepted, "Success"); 
       } 
      } 
     } 
    } 

、これは私のxamarin.androidでユーザースクリプトを作成します:ここで

は私のWeb APIコントローラーがあるので、私のせいではここで何

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net.Http; 
using System.Net.Http.Headers; 
using Newtonsoft.Json; 
using Android.App; 
using Android.Content; 
using Android.OS; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 

namespace LoginAzureDroid 
{ 

    public class NewUserActivity : Activity 
    { 
     EditText txtusername; 
     EditText txtPassword; 
     Button btncreate; 
     protected override void OnCreate(Bundle savedInstanceState) 
     { 
      base.OnCreate(savedInstanceState); 
      // Create your application here 
      SetContentView(Resource.Layout.Reg); 
      txtusername = FindViewById<EditText>(Resource.Id.txtsaveusername); 
      txtPassword = FindViewById<EditText>(Resource.Id.txtsavepassword); 
      btncreate = FindViewById<Button>(Resource.Id.btnsavecreate); 
      btncreate.Click += Btncreate_Click; 
     } 
     private async void Btncreate_Click(object sender, EventArgs e) 
     { 
      Login log = new Login(); 
      log.username = txtusername.Text; 
      log.password = txtPassword.Text; 
      HttpClient client = new HttpClient(); 
      string url = "http://xamarinlogin20170612105003.azurewebsites.net/api/Login"; 
      var uri = new Uri(url); 
      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 
      HttpResponseMessage response; 
      var json = JsonConvert.SerializeObject(log); 
      var content = new StringContent(json, Encoding.UTF8, "application/json"); 
      response = await client.PostAsync(uri, content); 
      if (response.StatusCode == System.Net.HttpStatusCode.Accepted) 
      { 
       var errorMessage1 = response.Content.ReadAsStringAsync().Result.Replace("\\", "").Trim(new char[1] 
       { 
       '"' 
       }); 
       Toast.MakeText(this, errorMessage1, ToastLength.Long).Show(); 
      } 
      else 
      { 
       var errorMessage1 = response.Content.ReadAsStringAsync().Result.Replace("\\", "").Trim(new char[1] 
       { 
       '"' 
       }); 
       Toast.MakeText(this, errorMessage1, ToastLength.Long).Show(); 
      } 
     } 
    } 
} 

を、それは私のURLですか? Web APIのコードですか?またはxamarin.androidの私のコード。

+0

ヘルプ誰ください –

+0

それはあなたのURLかもしれませ 'ます。http:// xamari/nlogin20170612105003.azurewebsites.net/API/Login'私は誰かがxamarinを分割想像することはできませんinto(xamari/n) –

答えて

1

{ "$のID": "1"、 "メッセージ": "いいえHTTPリソースが一致する見つからなかったリクエストURI 'http://xamari/nlogin20170612105003.azurewebsites.net/api/Login'。 「 『messageDetail』:」対処はあなたのコードに基づいて

を発見されなかった、あなたはLoginControllerためRoutePrefixを追加し、私はあなたが以下のように各またはあなたの行動のためのルート属性を追加する必要があると仮定:

string url = "http://xamarinlogin20170612105003.azurewebsites.net/api/Login/XAMARIN_REG"; 

をまた、あなたがより多くの詳細についてはAttribute Routing in ASP.NET Web API 2を参照してください可能性があり、次のように

[HttpGet] 
[Route("XAMARIN_Login")] 
// GET: api/Login/XAMARIN_Login?username={username}&password={password} 
public HttpResponseMessage Xamarin_login(string username, string password) 
[HttpPost] 
[Route("XAMARIN_REG")] 
// POST: api/Login/XAMARIN_REG 
public HttpResponseMessage Xamarin_reg(string username, string password) 
新しいユーザーを作成するためには、URLを変更する必要があります。

UPDATE:

次のように、ユーザ登録のためのビューモデルを定義することができます。

public class RegUser 
{ 
    public string username {get;set;} 
    public string password {get;set;} 
} 

[RoutePrefix("api/Login")] 
public class LoginController : ApiController 
{ 
    [HttpPost] 
    [Route("regUser")] 
    public string newUser(RegUser user) 
    { 
     return user.username + "_" + user.password; 
    } 
} 

enter image description here

また、ここではあなたが参照できpass multiple parameters to Web API controller methodsについてのブログは、ありますそれには複数のアプローチがあります。

+0

私はすでにそれを行いましたが、まだエラーメッセージが表示されています –

+0

アクションに関する同じエラーが表示されるか、新しいエラーメッセージが表示されますか? –

+0

いいえ、エラーメッセージは「要求URIと一致するHTTPリソースが見つかりませんでした」http://xamarinlogin20170612105003.azurewebsites.net/api/Login ' –

0

APIエンドポイントでは、URLに値(ユーザー名とパスワード)が送信されることを期待していますが、という名前のオブジェクトと投稿の本文に送信しています。これは、アクションは、私が先に説明内容を反映するために、このにあなたを修正するいずれかのあなたのAPIの署名を変更するには

を発見していない理由であるか、また

var parameters = new Dictionary<string, string> { { "username", txtusername.Text}, { "password", txtPassword.Text } }; 
var content = new FormUrlEncodedContent (parameters); 

response = await client.PostAsync(uri, content); 

のようなものにモバイルアプリのコードを変更します提案として、単一のHTTP操作のためにHttpClientを使用すると、完了したらオブジェクトを常に破棄してください。あなたはこのようusing句を使用してこれを取得することができます:

string url = "http://xamarinlogin20170612105003.azurewebsites.net/api/Login"; 
using (HttpClient client = new HttpClient()) 
{ 
    .... 

    var response = await client.PostAsync(new Uri(url), content); 

    .... 
} 
+0

私は私のAPIを変更する必要はありませんか? –

+0

@TheodorusAgumGumilang私はあなたに2つのオプションを与えました。オプション1 APIを変更する必要があります(Bruceの答えを参照してください)。オプション2; APIが予期していることを送信するときにAPIを変更する必要はありません。 – apineda

1

私は同じ問題がありますが、私はこの問題を解決しました。

は解決:

string url = "http://xamarinlogin20170612105003.azurewebsites.net/api/Login/XAMARIN_REG/username=" + user.username+ "&password=" + user.password"; 
関連する問題