0

I have created a new web application with MVC template and core references having MVC and Web API.

I want to handle all the business logic through Web API controller. I have a login page view, where on form submit i want to make a call to the Web API method with the form data.

Login.cshtml

<form action="/api/Authorization/Login" method="post">
<div>
    UserName: <input type="text" name="UserName" id="UserName" value="" />
</div>
<div>
    Password: <input type="password" name="UserPassword" id="UserPassword" value="" />
</div>
<div>
    <input type="submit" name="BtnSubmit" value="Login" />
</div>

I have added a Web API controller in the controller folder with the name 'Authorization' and on the above form submit, I'm not able to reach the method 'Login'.

Authorization Web API controller:

public class AuthorizationController : ApiController
{
    public ActionResult Login(string UserName, string UserPassword)
    {
        // method logic
    }
}

On form submit ending up with the following screen, enter image description here

Please let me know what am i missing here so that i can reach the method defined under the api controller?

Also have not added any new route configs, the only config present in webApiConfig.cs is,

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

1 Answer 1

1

You're missing a [HttpPost] attribute above your Login() function. As it is your method will only work for GET requests.

This page will help you out: https://www.asp.net/web-api/overview/advanced/sending-html-form-data-part-1

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.