How can I correctly implement a .NET controller method that handle POST request retrieving an URI parameter...





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0
















This question already has an answer here:




  • Is it possible to create a parameter binding on both FromURI and FromBody?

    1 answer




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?










share|improve this question















marked as duplicate by CodeCaster .net
Users with the  .net badge can single-handedly close .net questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 11:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    "I am pretty new in .NET" - you used that same excuse in 2014 already, so that doesn't fly. Read How to Ask and show what you have tried. Plenty of duplicate questions exist.

    – CodeCaster
    Nov 22 '18 at 10:59


















0
















This question already has an answer here:




  • Is it possible to create a parameter binding on both FromURI and FromBody?

    1 answer




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?










share|improve this question















marked as duplicate by CodeCaster .net
Users with the  .net badge can single-handedly close .net questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 11:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    "I am pretty new in .NET" - you used that same excuse in 2014 already, so that doesn't fly. Read How to Ask and show what you have tried. Plenty of duplicate questions exist.

    – CodeCaster
    Nov 22 '18 at 10:59














0












0








0


1







This question already has an answer here:




  • Is it possible to create a parameter binding on both FromURI and FromBody?

    1 answer




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?










share|improve this question

















This question already has an answer here:




  • Is it possible to create a parameter binding on both FromURI and FromBody?

    1 answer




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?





This question already has an answer here:




  • Is it possible to create a parameter binding on both FromURI and FromBody?

    1 answer








c# .net asp.net-mvc asp.net-apicontroller






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 11:00









CodeCaster

110k17148200




110k17148200










asked Nov 22 '18 at 10:20









AndreaNobiliAndreaNobili

13.5k59182344




13.5k59182344




marked as duplicate by CodeCaster .net
Users with the  .net badge can single-handedly close .net questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 11:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by CodeCaster .net
Users with the  .net badge can single-handedly close .net questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 '18 at 11:24


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    "I am pretty new in .NET" - you used that same excuse in 2014 already, so that doesn't fly. Read How to Ask and show what you have tried. Plenty of duplicate questions exist.

    – CodeCaster
    Nov 22 '18 at 10:59














  • 1





    "I am pretty new in .NET" - you used that same excuse in 2014 already, so that doesn't fly. Read How to Ask and show what you have tried. Plenty of duplicate questions exist.

    – CodeCaster
    Nov 22 '18 at 10:59








1




1





"I am pretty new in .NET" - you used that same excuse in 2014 already, so that doesn't fly. Read How to Ask and show what you have tried. Plenty of duplicate questions exist.

– CodeCaster
Nov 22 '18 at 10:59





"I am pretty new in .NET" - you used that same excuse in 2014 already, so that doesn't fly. Read How to Ask and show what you have tried. Plenty of duplicate questions exist.

– CodeCaster
Nov 22 '18 at 10:59












2 Answers
2






active

oldest

votes


















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");
}
}





share|improve this answer
























  • 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: docs.microsoft.com/en-us/aspnet/web-api/overview/… (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");
}
}





share|improve this answer
























  • 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): andrea-myapp.azurewebsites.net/api/MailProtocollo//protocollo/…

    – 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


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









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");
}
}





share|improve this answer
























  • 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: docs.microsoft.com/en-us/aspnet/web-api/overview/… (at bottom of page)

    – Bojan Delić
    Nov 22 '18 at 11:44
















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");
}
}





share|improve this answer
























  • 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: docs.microsoft.com/en-us/aspnet/web-api/overview/… (at bottom of page)

    – Bojan Delić
    Nov 22 '18 at 11:44














1












1








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");
}
}





share|improve this answer













Try this:



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



return Ok("TEST");
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 10:53









Bojan DelićBojan Delić

185




185













  • 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: docs.microsoft.com/en-us/aspnet/web-api/overview/… (at bottom of page)

    – Bojan Delić
    Nov 22 '18 at 11:44



















  • 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: docs.microsoft.com/en-us/aspnet/web-api/overview/… (at bottom of page)

    – Bojan Delić
    Nov 22 '18 at 11:44

















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





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: docs.microsoft.com/en-us/aspnet/web-api/overview/… (at bottom of page)

– Bojan Delić
Nov 22 '18 at 11:44





Check this: docs.microsoft.com/en-us/aspnet/web-api/overview/… (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");
}
}





share|improve this answer
























  • 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): andrea-myapp.azurewebsites.net/api/MailProtocollo//protocollo/…

    – 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
















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");
}
}





share|improve this answer
























  • 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): andrea-myapp.azurewebsites.net/api/MailProtocollo//protocollo/…

    – 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














1












1








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");
}
}





share|improve this answer













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");
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 10:58









Péter CsajtaiPéter Csajtai

63617




63617













  • 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): andrea-myapp.azurewebsites.net/api/MailProtocollo//protocollo/…

    – 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



















  • 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): andrea-myapp.azurewebsites.net/api/MailProtocollo//protocollo/…

    – 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

















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





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





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): andrea-myapp.azurewebsites.net/api/MailProtocollo//protocollo/…

– AndreaNobili
Nov 22 '18 at 11:27





I am calling on an URL like this (the ? param are mandatory in my application): andrea-myapp.azurewebsites.net/api/MailProtocollo//protocollo/…

– 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





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



Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents