How to use POST in ApiController

Cody Jones

I have searched for at least an hour and a half now and I'm not any closer to learning how to use POST methods in my ApiController. I need an effective way of using post to create a login system that will search my database based on the username/password combination and create a JSON object that I can send back to my web page. Any resources on using post? I've tried to acomplish this with get but I can use any variables more than 'ID'

public IHttpActionResult GetLogin(string id)
        {
            //Query Database for Unique username.

            if (id == "mager1794")
            {

                //Create Login class with username, and password details.
                return Ok( new Models.Login() { id = 1, userName = "mager1794", passWord = "*******" });
            }


            return Ok(-1);
        }

This is what I have for my Get method but I'm just not having any luck creating a POST version of this.

šljaker

Maybe something like this:

[RoutePrefix("api/account")]
public class AccountController : ApiController
{
    public class LoginInfo
    {
        [Required]
        public string Username { get; set; }

        [Required]
        public string Password { get; set; }
    }

    [Route("login")]
    [HttpPost]
    public IHttpActionResult AuthenticateUser(LoginInfo loginInfo)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (!Membership.ValidateUser(loginInfo.Username, loginInfo.Password))
        {
            ModelState.AddModelError("", "Incorrect username or password");
            return BadRequest(ModelState);
        }

        FormsAuthentication.SetAuthCookie(loginInfo.Username, true);

        return Ok();
    }
}

Client side:

<form action="#" id="login-form">
    <label for="username">Username:</label>
    <input type="text" name="username" id="username"/>

    <label for="password">Password:</label>
    <input type="password" name="password" id="password"/>
    <div><input type="submit"/></div>
</form>

<script>
   $(document).ready(function () {
       $("#login-form").submit(function (e) {
           e.preventDefault();

           var username = $('#username').val();
           var password = $('#password').val();

           $.ajax({
               type: 'POST',
               url: '/api/account/Login/',
               data: { Username: username, Password: password },
               success: function () {
                   // refresh the page if username and password are correct
                   location.reload();
               }
           });
       });
   });
</script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to retrieve POST body data in ApiController?

From Dev

ApiController receives null in POST

From Dev

How to use session in apicontroller in asp.net mvc 4

From Dev

Get raw post request in an ApiController

From Dev

How to test apicontroller?

From Dev

How to access Response in ApiController

From Dev

How to access Response in ApiController

From Dev

How can I upload an image and POST data to an Azure Mobile Services ApiController endpoint?

From Dev

Bitbucket POST Hooks with .Net ApiController setup?

From Dev

How to Insert Data in CakePHP with APIController?

From Dev

Is it possible to use an ApiController to call an Action written in a Controller?

From Dev

How to use HttpClient to Post with Authentication

From Dev

Bootstrap form, how to use with POST

From Dev

How to use variables in a curl post?

From Dev

How to use JQuery post method

From Dev

How to use the response of Ajax Post

From Dev

Bootstrap form, how to use with POST

From Dev

how to use AJAX post in django

From Dev

Http.post from Angular 2 to ASP.NET ApiController

From Dev

.NET (ApiController) / jQuery .ajax : what to return from POST?

From Dev

Visual Studio 2015 / IIS Express and ApiController POST events

From Dev

How to call a method in ApiController with http request?

From Dev

How do I create a new ApiController with HttpContext?

From Dev

How does the ApiController class work and behave?

From Dev

Asynchronous methods of ApiController -- what's the profit? When to use?

From Dev

Can I use a Web ApiController to fetch a SelectList for a Html.DropDownList?

From Dev

Use both MVC Controller and Web ApiController in MVC project?

From Dev

how would I post multiple values using $_Post and then use these values

From Dev

How to use $_POST correctly -- can't get button to work with POST

Related Related

  1. 1

    How to retrieve POST body data in ApiController?

  2. 2

    ApiController receives null in POST

  3. 3

    How to use session in apicontroller in asp.net mvc 4

  4. 4

    Get raw post request in an ApiController

  5. 5

    How to test apicontroller?

  6. 6

    How to access Response in ApiController

  7. 7

    How to access Response in ApiController

  8. 8

    How can I upload an image and POST data to an Azure Mobile Services ApiController endpoint?

  9. 9

    Bitbucket POST Hooks with .Net ApiController setup?

  10. 10

    How to Insert Data in CakePHP with APIController?

  11. 11

    Is it possible to use an ApiController to call an Action written in a Controller?

  12. 12

    How to use HttpClient to Post with Authentication

  13. 13

    Bootstrap form, how to use with POST

  14. 14

    How to use variables in a curl post?

  15. 15

    How to use JQuery post method

  16. 16

    How to use the response of Ajax Post

  17. 17

    Bootstrap form, how to use with POST

  18. 18

    how to use AJAX post in django

  19. 19

    Http.post from Angular 2 to ASP.NET ApiController

  20. 20

    .NET (ApiController) / jQuery .ajax : what to return from POST?

  21. 21

    Visual Studio 2015 / IIS Express and ApiController POST events

  22. 22

    How to call a method in ApiController with http request?

  23. 23

    How do I create a new ApiController with HttpContext?

  24. 24

    How does the ApiController class work and behave?

  25. 25

    Asynchronous methods of ApiController -- what's the profit? When to use?

  26. 26

    Can I use a Web ApiController to fetch a SelectList for a Html.DropDownList?

  27. 27

    Use both MVC Controller and Web ApiController in MVC project?

  28. 28

    how would I post multiple values using $_Post and then use these values

  29. 29

    How to use $_POST correctly -- can't get button to work with POST

HotTag

Archive