Reputation: 11
i'm kinda new to programming c# and asp.net, i just want to build a rest api for my android app to sign in and sign up. but i face a problem for sign in (login).
the problem is i just wrote this codes below:
namespace CPanel.Controllers
{
public class DashboardController : Controller
{
// GET: Dashboard
public ActionResult CreateUser()
{
return View();
}
[HttpPost]
public ActionResult CreateUser(string username,string password)
{
CPanel.Models.CPanelEntities1 db = new Models.CPanelEntities1();
db.USP_AddUSer(username, password);
return View();
}
[HttpPost]
public ActionResult login(string username,string password)
{
CPanel.Models.CPanelEntities1 db = new Models.CPanelEntities1();
try
{
var user = db.USP_Authenticate(username, password).First();
return Json(new { UserId = user.UserId,Username=user.Username,IsAdmin=user.IsAdmin,Message="ok"});
}catch(Exception)
{
return Json(new { message = "error" });
}
}
}
}
the first part (CreateUser) work perfectly. but second part (login) only work on "Postman" chrome application.
i post a request on "Postman" -
localhost/dashboard/login?username=php&password=php
and i see a json:
{ "UserId": 29, "Username": "php", "IsAdmin": false, "Message": "ok" }
in localhost and wwwroot (iis) there in no chance and i face this error:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /dashboard/login
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1586.0
Upvotes: 0
Views: 684
Reputation: 1242
When you enter the http://localhost/dashboard/login?username=php&password=php
url to web browser it issues a GET request, however your controller only accepts POST requests.
There's another thing, as far as I understand you need a web api but your example is a standard ASP.NET MVC controller. You might want to take a look on the following tutorial: https://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
Upvotes: 2