-1

I have to develop a REST API that have to handle POST request (taking a payload from the body of my request) and at the same time taking an URI parameter.

Into my controller class I have something like this:

public class MailProtocolloController : ApiController
{
    [SharePointContextWebAPIFilter]
    [HttpPost]
    [ActionName("protocollo/mail")]
    public IHttpActionResult InviaAlProtocollo(XXX)
    {



        return Ok("TEST");
    }
}

My problems are:

  1. As you can see I am using the ActionName annotation to specify the URL path handled by this controller method. How can I specify the URI parameter into this ActionName annotation? (can I?) It have to handle something like this: protocollo/mail/{id}

  2. What input parameter have to be specified for this controller method? One for the ID retrieved from the URI (a string) and an object for the request payload? Or this payload can be retrieved inside the method?

How can I implement this behavior?

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
  • 1
    _"I am pretty new in .NET"_ - you used [that same excuse in 2014 already](https://stackoverflow.com/questions/24265644/why-in-this-c-sharp-net-controller-method-do-i-have-this-signature), so that doesn't fly. Read [ask] and show what you have tried. Plenty of duplicate questions exist. – CodeCaster Nov 22 '18 at 10:59

2 Answers2

1

Try this:

public class MailProtocolloController : ApiController
{
    [SharePointContextWebAPIFilter]
    [HttpPost]
    [Route("protocollo/mail/{id}")]
    public IHttpActionResult InviaAlProtocollo([FromUri] string id, [FromBody] string context)
    {



        return Ok("TEST");
    }
}
  • calling my API I am obtaining this error: The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. – AndreaNobili Nov 22 '18 at 11:08
  • Check this: https://learn.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2 (at bottom of page) – Bojan Delić Nov 22 '18 at 11:44
1

You can use the RouteAttribute to define route mappings where you can use parameter placeholders to get those from the url path.

And you can also get the body of the request if you decorate your parameter which represents the body with the FromBody attribute:

[RoutePrefix("protocollo")]
public class MailProtocolloController : ApiController
{
    [SharePointContextWebAPIFilter]
    [HttpPost]
    [Route("mail/{id}")]
    public IHttpActionResult InviaAlProtocollo(string id, [FromBody]string body)
    {
        return Ok("TEST");
    }
}
Péter Csajtai
  • 878
  • 6
  • 9
  • calling my API I am obtaining this error: The resource you are looking for has been removed, had its name changed, or is temporarily unavailable – AndreaNobili Nov 22 '18 at 11:09
  • How do you calling the API? Could you send an example, because there could be a lot reasons why are you getting this response. – Péter Csajtai Nov 22 '18 at 11:13
  • I am calling on an URL like this (the ? param are mandatory in my application): https://andrea-myapp.azurewebsites.net/api/MailProtocollo//protocollo/mail/xxx?SPHostUrl=https://mycompany.sharepoint.com/sites/testsite/MyPec&SPLanguage=it-IT&SPClientTag=0&SPProductNumber=1 – AndreaNobili Nov 22 '18 at 11:27
  • `api/MailProtocollo/protocollo/mail/xxx` is this a valid route? I assume that you don't need the `MailProtocollo` url part, how is your route mapping exactly looking now? – Péter Csajtai Nov 22 '18 at 11:40