0

I have a .NET project that has a Web API portion to it which should be accepting requests externally. The action accepts an object which is then used further on. The problem is when testing with postman app, the action is hit but variables are not passed.

Here is an example of my URL in postman: http://localhost:12345/api/login/postlogin?Username=un&Password=pw

I have tried using [frombody], this would work only if I am receiving a single string. My application will be utilizing lots of data passing back and forth. I have also tried multiple solutions on StackOverflow including [FromUri]. The post is still null.

WebAPi.Config snippet:
 public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            config.EnableCors();
            // Web API routes
            config.MapHttpAttributeRoutes();

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

        // POST: api/postLogin/
        [AllowAnonymous]
        [System.Web.Http.HttpPost, System.Web.Http.Route("postlogin")]
        public async Task<IHttpActionResult> PostLogin(LoginDTO login)
        {

        }

The expected result as stated early is to receive variables as an object then use them within the action.

robdigm
  • 143
  • 2
  • 16
  • Possible duplicate of [Why do we have to specify FromBody and FromUri?](https://stackoverflow.com/questions/24625303/why-do-we-have-to-specify-frombody-and-fromuri) – EdSF May 23 '19 at 18:52
  • I tried that. And the same response. – robdigm May 23 '19 at 19:23
  • @robdigm you are doing it all wrong, you should put it your model into the body for `POST`.. not in the url.. in the url is just por `GET` – Alexandre Rodrigues May 23 '19 at 21:00

1 Answers1

1

Your configuration for Routing is correct. You should use [FromBody] attribute to the parameter, because this is a POST request. you should never use GET request to do authentication!

public async Task<IHttpActionResult> PostLogin([FromBody]LoginDTO login)

Postman post