0

I'm trying to create an address object in a database through an asp.net core api. I'm using Postman to invoke the method.

Class:

namespace LC.Tools.API.Controllers {
    [Route("api/[controller]")]
    public class MailerController : Controller
    {
        Data.LCToolsDbContext _context;

        public MailerController(Data.LCToolsDbContext context)
        {
            _context = context;
        }

        [HttpPost]
        public async Task<IActionResult> CreateAddress(int client, string name, string email)
        {
            Address adr = new Address() { ClientId = client, Name = name, Email = email };
            _context.MailerAddresses.Add(adr);
            await _context.SaveChangesAsync();

            return Ok(adr);
        }
    } }

URL (using POST):

http://localhost:50444/api/mailer/createAddress?client=1&name=test&[email protected]

I also have a break point in the method, but it never hits. I don't get any error message, it just doesn't do anything.

14
  • can you show the View? Commented May 30, 2017 at 7:40
  • Have a look at URL Encoding your values. An @ sign is not valid in a URL. Use %40, you'll be fine. Commented May 30, 2017 at 7:41
  • Why not pass in the address object? Commented May 30, 2017 at 7:41
  • [Route("api/[controller]")] add this Commented May 30, 2017 at 7:41
  • 1
    try post to localhost:50444/api/… Commented May 30, 2017 at 8:01

5 Answers 5

3

You can see @Rick van den Bosch's comment but still you would like to specify the route with action. use this

 [Route("api/[controller]/[action]")] 
Sign up to request clarification or add additional context in comments.

Comments

1

See @Ayvaras' comment. Since you're building a Web API controller, you don't have to specify the action name in your URL. The HttpPost points all POST actions to the mailer controller towards the CreateAddress method.

Your POST URL should be:

http://localhost:50444/api/mailer?client=1&name=test&[email protected]

Comments

1

Problem solved! Thanks Ayvaras,

[Route("api/[controller]/[action]")]

did the trick. Now that the method is found I can look into how to pass an object instead of using querystring

Comments

0

You are not using POST correctly.

If you use POST you should not be sending parameters through the query string, you should use the request body. Even though, if you still have to send them via query string you should use the FromQuery attribute in your action parameters.

[HttpPost]
public async Task<IActionResult> CreateAddress([FromQuery] int client, [FromQuery] string name, [FromQuery] string email)
{
    Address adr = new Address() { ClientId = client, Name = name, Email = email };
    _context.MailerAddresses.Add(adr);
    await _context.SaveChangesAsync();

    return Ok(adr);
}

Edit: Use [Route("api/[controller]/[action]")] to append the action name in your action route.

2 Comments

POST is not required to use the HTTP body only to communicate data. Just like you use the rest of the URL, you can just use query arguments at any moment, for any HTTP method. That being said, according to the comments, the endpoint is not found, so your suggestion will not help here.
@poke It's not required but it's not a convention. If a customer or developer has to implement this API it won't look like the usual thing to use POST but send parameters via query string.
0

Instead of changing the controllers routing scheme, it's also possible to specify the endpoint name by doing the following

[HttpPost("createAddress")]
public async Task<IActionResult> CreateAddress(int client, string name, string email)
{
    [...]
}

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.