Reputation: 1486
I tried to make new user in my sql azure in xamarin.android with web api. but i got this error message every time I try to create new user
{"$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
so this web api is provide to make authentication and create new user, my login is fine no error, but when I create new user I got that error message.
Here is my web api controller :
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");
}
}
}
}
and this is my create user script in xamarin.android :
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();
}
}
}
}
so what is my fault in here, is it my url ? my code in web api ? or my code in xamarin.android.
Upvotes: 0
Views: 592
Reputation: 686
i have same problem but i solved the issue.
Solve:
string url = "http://xamarinlogin20170612105003.azurewebsites.net/api/Login/XAMARIN_REG/username=" + user.username+ "&password=" + user.password";
Upvotes: 1
Reputation: 18465
{"$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
Based on your code, you added the RoutePrefix for your LoginController
, I assumed that you need to add Route Attribute for each or your actions as follows:
[HttpPost]
[Route("XAMARIN_REG")]
// POST: api/Login/XAMARIN_REG
public HttpResponseMessage Xamarin_reg(string username, string password)
[HttpGet]
[Route("XAMARIN_Login")]
// GET: api/Login/XAMARIN_Login?username={username}&password={password}
public HttpResponseMessage Xamarin_login(string username, string password)
For creating a new user, you need to change the url as follows:
string url = "http://xamarinlogin20170612105003.azurewebsites.net/api/Login/XAMARIN_REG";
Also, you could refer to Attribute Routing in ASP.NET Web API 2 for more details.
UPDATE:
You could define a view model for user registration as follows:
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;
}
}
Additionally, here is a blog about pass multiple parameters to Web API controller methods, you could refer to it for multiple approaches.
Upvotes: 1
Reputation: 9346
Your API endpoint is expecting the values (username and password) to be sent in the url but you are sending them in an object called Login
and in the body of the post. This is the reason an Action is not found
To fix this you either change your API signature to reflect what I explained above or you change the code in the Mobile App to something like
var parameters = new Dictionary<string, string> { { "username", txtusername.Text}, { "password", txtPassword.Text } };
var content = new FormUrlEncodedContent (parameters);
response = await client.PostAsync(uri, content);
Also as a suggestion, when using HttpClient
for a single HTTP operation try to always dispose the object once you finish. You can get this by using the using
clause like this:
string url = "http://xamarinlogin20170612105003.azurewebsites.net/api/Login";
using (HttpClient client = new HttpClient())
{
....
var response = await client.PostAsync(new Uri(url), content);
....
}
Upvotes: 0