How to return custom message in web API Post method












2















I would like the following Post method to return a failure by assigning a failure value to the "result" variable but I am not sure how to achieve that. Ideally it would say that the installation id is invalid, but not sure I could do that:



[Authorize]
[HttpPost, Route("sendForDevelopment")]
public async Task<NotificationOutcome> Post([FromBody]string message, string installationId)
{

string hubName = "myHubName";
string hubNameDefaultShared = "myHubNameDefaultShared";

NotificationHubClient hub = NotificationHubClient
.CreateClientFromConnectionString(hubNameDefaultShared, hubName, enableTestSend: true);

var templateParams = new Dictionary<string, string>
{
["messageParam"] = message
};

NotificationOutcome result = null;

if (string.IsNullOrWhiteSpace(installationId))
{
// output a installation id is null or empty message or assign failure to the result variable
}
else
{
result = await hub.SendTemplateNotificationAsync(templateParams, "$InstallationId:{" + installationId + "}").ConfigureAwait(false);
}

return result;
}









share|improve this question

























  • have the result of the action be a IHttpActionResult derived object. That should allow greater flexibility as to what you can return

    – Nkosi
    Nov 14 '18 at 22:51











  • @Nkosi I was thinking something among those lines. if the result of the action is IHttpActionResult, it would also include same message in case of success as NotificationOutcomeResult or maybe similar?

    – EmilRR1
    Nov 14 '18 at 22:53













  • Well then just create an instance of NotificationOutcome and populate as needed

    – Nkosi
    Nov 14 '18 at 22:55











  • @Nkosi i wasnt able to populate, i checkout the class definiton for NotificationOutcome but didnt figure out how to assign it a failure manually.

    – EmilRR1
    Nov 14 '18 at 22:56











  • @Nkosi also I just tried IHttpActionResult but I get an error in the line result = await hub.SendTemplateNotificationAsync(..) saying that it cannot implicitly convert NotificationOutcome to IHttpActionResult

    – EmilRR1
    Nov 14 '18 at 22:57
















2















I would like the following Post method to return a failure by assigning a failure value to the "result" variable but I am not sure how to achieve that. Ideally it would say that the installation id is invalid, but not sure I could do that:



[Authorize]
[HttpPost, Route("sendForDevelopment")]
public async Task<NotificationOutcome> Post([FromBody]string message, string installationId)
{

string hubName = "myHubName";
string hubNameDefaultShared = "myHubNameDefaultShared";

NotificationHubClient hub = NotificationHubClient
.CreateClientFromConnectionString(hubNameDefaultShared, hubName, enableTestSend: true);

var templateParams = new Dictionary<string, string>
{
["messageParam"] = message
};

NotificationOutcome result = null;

if (string.IsNullOrWhiteSpace(installationId))
{
// output a installation id is null or empty message or assign failure to the result variable
}
else
{
result = await hub.SendTemplateNotificationAsync(templateParams, "$InstallationId:{" + installationId + "}").ConfigureAwait(false);
}

return result;
}









share|improve this question

























  • have the result of the action be a IHttpActionResult derived object. That should allow greater flexibility as to what you can return

    – Nkosi
    Nov 14 '18 at 22:51











  • @Nkosi I was thinking something among those lines. if the result of the action is IHttpActionResult, it would also include same message in case of success as NotificationOutcomeResult or maybe similar?

    – EmilRR1
    Nov 14 '18 at 22:53













  • Well then just create an instance of NotificationOutcome and populate as needed

    – Nkosi
    Nov 14 '18 at 22:55











  • @Nkosi i wasnt able to populate, i checkout the class definiton for NotificationOutcome but didnt figure out how to assign it a failure manually.

    – EmilRR1
    Nov 14 '18 at 22:56











  • @Nkosi also I just tried IHttpActionResult but I get an error in the line result = await hub.SendTemplateNotificationAsync(..) saying that it cannot implicitly convert NotificationOutcome to IHttpActionResult

    – EmilRR1
    Nov 14 '18 at 22:57














2












2








2


0






I would like the following Post method to return a failure by assigning a failure value to the "result" variable but I am not sure how to achieve that. Ideally it would say that the installation id is invalid, but not sure I could do that:



[Authorize]
[HttpPost, Route("sendForDevelopment")]
public async Task<NotificationOutcome> Post([FromBody]string message, string installationId)
{

string hubName = "myHubName";
string hubNameDefaultShared = "myHubNameDefaultShared";

NotificationHubClient hub = NotificationHubClient
.CreateClientFromConnectionString(hubNameDefaultShared, hubName, enableTestSend: true);

var templateParams = new Dictionary<string, string>
{
["messageParam"] = message
};

NotificationOutcome result = null;

if (string.IsNullOrWhiteSpace(installationId))
{
// output a installation id is null or empty message or assign failure to the result variable
}
else
{
result = await hub.SendTemplateNotificationAsync(templateParams, "$InstallationId:{" + installationId + "}").ConfigureAwait(false);
}

return result;
}









share|improve this question
















I would like the following Post method to return a failure by assigning a failure value to the "result" variable but I am not sure how to achieve that. Ideally it would say that the installation id is invalid, but not sure I could do that:



[Authorize]
[HttpPost, Route("sendForDevelopment")]
public async Task<NotificationOutcome> Post([FromBody]string message, string installationId)
{

string hubName = "myHubName";
string hubNameDefaultShared = "myHubNameDefaultShared";

NotificationHubClient hub = NotificationHubClient
.CreateClientFromConnectionString(hubNameDefaultShared, hubName, enableTestSend: true);

var templateParams = new Dictionary<string, string>
{
["messageParam"] = message
};

NotificationOutcome result = null;

if (string.IsNullOrWhiteSpace(installationId))
{
// output a installation id is null or empty message or assign failure to the result variable
}
else
{
result = await hub.SendTemplateNotificationAsync(templateParams, "$InstallationId:{" + installationId + "}").ConfigureAwait(false);
}

return result;
}






c# asp.net-web-api push-notification






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 23:31









Nkosi

116k16131195




116k16131195










asked Nov 14 '18 at 22:48









EmilRR1EmilRR1

10516




10516













  • have the result of the action be a IHttpActionResult derived object. That should allow greater flexibility as to what you can return

    – Nkosi
    Nov 14 '18 at 22:51











  • @Nkosi I was thinking something among those lines. if the result of the action is IHttpActionResult, it would also include same message in case of success as NotificationOutcomeResult or maybe similar?

    – EmilRR1
    Nov 14 '18 at 22:53













  • Well then just create an instance of NotificationOutcome and populate as needed

    – Nkosi
    Nov 14 '18 at 22:55











  • @Nkosi i wasnt able to populate, i checkout the class definiton for NotificationOutcome but didnt figure out how to assign it a failure manually.

    – EmilRR1
    Nov 14 '18 at 22:56











  • @Nkosi also I just tried IHttpActionResult but I get an error in the line result = await hub.SendTemplateNotificationAsync(..) saying that it cannot implicitly convert NotificationOutcome to IHttpActionResult

    – EmilRR1
    Nov 14 '18 at 22:57



















  • have the result of the action be a IHttpActionResult derived object. That should allow greater flexibility as to what you can return

    – Nkosi
    Nov 14 '18 at 22:51











  • @Nkosi I was thinking something among those lines. if the result of the action is IHttpActionResult, it would also include same message in case of success as NotificationOutcomeResult or maybe similar?

    – EmilRR1
    Nov 14 '18 at 22:53













  • Well then just create an instance of NotificationOutcome and populate as needed

    – Nkosi
    Nov 14 '18 at 22:55











  • @Nkosi i wasnt able to populate, i checkout the class definiton for NotificationOutcome but didnt figure out how to assign it a failure manually.

    – EmilRR1
    Nov 14 '18 at 22:56











  • @Nkosi also I just tried IHttpActionResult but I get an error in the line result = await hub.SendTemplateNotificationAsync(..) saying that it cannot implicitly convert NotificationOutcome to IHttpActionResult

    – EmilRR1
    Nov 14 '18 at 22:57

















have the result of the action be a IHttpActionResult derived object. That should allow greater flexibility as to what you can return

– Nkosi
Nov 14 '18 at 22:51





have the result of the action be a IHttpActionResult derived object. That should allow greater flexibility as to what you can return

– Nkosi
Nov 14 '18 at 22:51













@Nkosi I was thinking something among those lines. if the result of the action is IHttpActionResult, it would also include same message in case of success as NotificationOutcomeResult or maybe similar?

– EmilRR1
Nov 14 '18 at 22:53







@Nkosi I was thinking something among those lines. if the result of the action is IHttpActionResult, it would also include same message in case of success as NotificationOutcomeResult or maybe similar?

– EmilRR1
Nov 14 '18 at 22:53















Well then just create an instance of NotificationOutcome and populate as needed

– Nkosi
Nov 14 '18 at 22:55





Well then just create an instance of NotificationOutcome and populate as needed

– Nkosi
Nov 14 '18 at 22:55













@Nkosi i wasnt able to populate, i checkout the class definiton for NotificationOutcome but didnt figure out how to assign it a failure manually.

– EmilRR1
Nov 14 '18 at 22:56





@Nkosi i wasnt able to populate, i checkout the class definiton for NotificationOutcome but didnt figure out how to assign it a failure manually.

– EmilRR1
Nov 14 '18 at 22:56













@Nkosi also I just tried IHttpActionResult but I get an error in the line result = await hub.SendTemplateNotificationAsync(..) saying that it cannot implicitly convert NotificationOutcome to IHttpActionResult

– EmilRR1
Nov 14 '18 at 22:57





@Nkosi also I just tried IHttpActionResult but I get an error in the line result = await hub.SendTemplateNotificationAsync(..) saying that it cannot implicitly convert NotificationOutcome to IHttpActionResult

– EmilRR1
Nov 14 '18 at 22:57












1 Answer
1






active

oldest

votes


















2














Have the result of the action be a IHttpActionResult derived object.



That should allow greater flexibility as to what you can return when the request is not valid



For example



[Authorize]
[HttpPost, Route("sendForDevelopment")]
public async Task<IHttpActionResult> Post([FromBody]string message, string installationId) {

if (string.IsNullOrWhiteSpace(installationId)) {
var model = new {
error = new {
code = 400,
message = "installation id is null or empty"
}
}
return Content(HttpStatusCode.Badrequest, model); //400 Bad Request with error message
}

string hubName = "myHubName";
string hubNameDefaultShared = "myHubNameDefaultShared";

var hub = NotificationHubClient
.CreateClientFromConnectionString(hubNameDefaultShared, hubName, enableTestSend: true);

var templateParams = new Dictionary<string, string>
{
["messageParam"] = message
};

NotificationOutcome result = await hub.SendTemplateNotificationAsync(templateParams, "$InstallationId:{" + installationId + "}").ConfigureAwait(false);
return Ok(result); //200 OK with model result
}


For a bad request the response body would look something like



{
"error": {
"code": 400,
"message": "installation id is null or empty"
}
}


On the client side you check the status code of the response and proceed accordingly.



var response = await client.PostAsync(url, content);
if(response.IsSuccessStatusCode)
var result = await response.Content.ReadAsAsync<NotificationOutcomeResult>();

//...
else {
//...check why request failed.
var model = await response.Content.ReadAsAsync<ErrorResponse>();

var message = model.error.message;

//...
}

//...





share|improve this answer


























  • thank you, giving it a try. Looks awesome though.

    – EmilRR1
    Nov 14 '18 at 23:04






  • 1





    @EmilRR1 I made some updates to the original answer.

    – Nkosi
    Nov 15 '18 at 0:03













  • Worked perfectly.

    – EmilRR1
    Nov 19 '18 at 22:11











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309884%2fhow-to-return-custom-message-in-web-api-post-method%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Have the result of the action be a IHttpActionResult derived object.



That should allow greater flexibility as to what you can return when the request is not valid



For example



[Authorize]
[HttpPost, Route("sendForDevelopment")]
public async Task<IHttpActionResult> Post([FromBody]string message, string installationId) {

if (string.IsNullOrWhiteSpace(installationId)) {
var model = new {
error = new {
code = 400,
message = "installation id is null or empty"
}
}
return Content(HttpStatusCode.Badrequest, model); //400 Bad Request with error message
}

string hubName = "myHubName";
string hubNameDefaultShared = "myHubNameDefaultShared";

var hub = NotificationHubClient
.CreateClientFromConnectionString(hubNameDefaultShared, hubName, enableTestSend: true);

var templateParams = new Dictionary<string, string>
{
["messageParam"] = message
};

NotificationOutcome result = await hub.SendTemplateNotificationAsync(templateParams, "$InstallationId:{" + installationId + "}").ConfigureAwait(false);
return Ok(result); //200 OK with model result
}


For a bad request the response body would look something like



{
"error": {
"code": 400,
"message": "installation id is null or empty"
}
}


On the client side you check the status code of the response and proceed accordingly.



var response = await client.PostAsync(url, content);
if(response.IsSuccessStatusCode)
var result = await response.Content.ReadAsAsync<NotificationOutcomeResult>();

//...
else {
//...check why request failed.
var model = await response.Content.ReadAsAsync<ErrorResponse>();

var message = model.error.message;

//...
}

//...





share|improve this answer


























  • thank you, giving it a try. Looks awesome though.

    – EmilRR1
    Nov 14 '18 at 23:04






  • 1





    @EmilRR1 I made some updates to the original answer.

    – Nkosi
    Nov 15 '18 at 0:03













  • Worked perfectly.

    – EmilRR1
    Nov 19 '18 at 22:11
















2














Have the result of the action be a IHttpActionResult derived object.



That should allow greater flexibility as to what you can return when the request is not valid



For example



[Authorize]
[HttpPost, Route("sendForDevelopment")]
public async Task<IHttpActionResult> Post([FromBody]string message, string installationId) {

if (string.IsNullOrWhiteSpace(installationId)) {
var model = new {
error = new {
code = 400,
message = "installation id is null or empty"
}
}
return Content(HttpStatusCode.Badrequest, model); //400 Bad Request with error message
}

string hubName = "myHubName";
string hubNameDefaultShared = "myHubNameDefaultShared";

var hub = NotificationHubClient
.CreateClientFromConnectionString(hubNameDefaultShared, hubName, enableTestSend: true);

var templateParams = new Dictionary<string, string>
{
["messageParam"] = message
};

NotificationOutcome result = await hub.SendTemplateNotificationAsync(templateParams, "$InstallationId:{" + installationId + "}").ConfigureAwait(false);
return Ok(result); //200 OK with model result
}


For a bad request the response body would look something like



{
"error": {
"code": 400,
"message": "installation id is null or empty"
}
}


On the client side you check the status code of the response and proceed accordingly.



var response = await client.PostAsync(url, content);
if(response.IsSuccessStatusCode)
var result = await response.Content.ReadAsAsync<NotificationOutcomeResult>();

//...
else {
//...check why request failed.
var model = await response.Content.ReadAsAsync<ErrorResponse>();

var message = model.error.message;

//...
}

//...





share|improve this answer


























  • thank you, giving it a try. Looks awesome though.

    – EmilRR1
    Nov 14 '18 at 23:04






  • 1





    @EmilRR1 I made some updates to the original answer.

    – Nkosi
    Nov 15 '18 at 0:03













  • Worked perfectly.

    – EmilRR1
    Nov 19 '18 at 22:11














2












2








2







Have the result of the action be a IHttpActionResult derived object.



That should allow greater flexibility as to what you can return when the request is not valid



For example



[Authorize]
[HttpPost, Route("sendForDevelopment")]
public async Task<IHttpActionResult> Post([FromBody]string message, string installationId) {

if (string.IsNullOrWhiteSpace(installationId)) {
var model = new {
error = new {
code = 400,
message = "installation id is null or empty"
}
}
return Content(HttpStatusCode.Badrequest, model); //400 Bad Request with error message
}

string hubName = "myHubName";
string hubNameDefaultShared = "myHubNameDefaultShared";

var hub = NotificationHubClient
.CreateClientFromConnectionString(hubNameDefaultShared, hubName, enableTestSend: true);

var templateParams = new Dictionary<string, string>
{
["messageParam"] = message
};

NotificationOutcome result = await hub.SendTemplateNotificationAsync(templateParams, "$InstallationId:{" + installationId + "}").ConfigureAwait(false);
return Ok(result); //200 OK with model result
}


For a bad request the response body would look something like



{
"error": {
"code": 400,
"message": "installation id is null or empty"
}
}


On the client side you check the status code of the response and proceed accordingly.



var response = await client.PostAsync(url, content);
if(response.IsSuccessStatusCode)
var result = await response.Content.ReadAsAsync<NotificationOutcomeResult>();

//...
else {
//...check why request failed.
var model = await response.Content.ReadAsAsync<ErrorResponse>();

var message = model.error.message;

//...
}

//...





share|improve this answer















Have the result of the action be a IHttpActionResult derived object.



That should allow greater flexibility as to what you can return when the request is not valid



For example



[Authorize]
[HttpPost, Route("sendForDevelopment")]
public async Task<IHttpActionResult> Post([FromBody]string message, string installationId) {

if (string.IsNullOrWhiteSpace(installationId)) {
var model = new {
error = new {
code = 400,
message = "installation id is null or empty"
}
}
return Content(HttpStatusCode.Badrequest, model); //400 Bad Request with error message
}

string hubName = "myHubName";
string hubNameDefaultShared = "myHubNameDefaultShared";

var hub = NotificationHubClient
.CreateClientFromConnectionString(hubNameDefaultShared, hubName, enableTestSend: true);

var templateParams = new Dictionary<string, string>
{
["messageParam"] = message
};

NotificationOutcome result = await hub.SendTemplateNotificationAsync(templateParams, "$InstallationId:{" + installationId + "}").ConfigureAwait(false);
return Ok(result); //200 OK with model result
}


For a bad request the response body would look something like



{
"error": {
"code": 400,
"message": "installation id is null or empty"
}
}


On the client side you check the status code of the response and proceed accordingly.



var response = await client.PostAsync(url, content);
if(response.IsSuccessStatusCode)
var result = await response.Content.ReadAsAsync<NotificationOutcomeResult>();

//...
else {
//...check why request failed.
var model = await response.Content.ReadAsAsync<ErrorResponse>();

var message = model.error.message;

//...
}

//...






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 23:35

























answered Nov 14 '18 at 23:03









NkosiNkosi

116k16131195




116k16131195













  • thank you, giving it a try. Looks awesome though.

    – EmilRR1
    Nov 14 '18 at 23:04






  • 1





    @EmilRR1 I made some updates to the original answer.

    – Nkosi
    Nov 15 '18 at 0:03













  • Worked perfectly.

    – EmilRR1
    Nov 19 '18 at 22:11



















  • thank you, giving it a try. Looks awesome though.

    – EmilRR1
    Nov 14 '18 at 23:04






  • 1





    @EmilRR1 I made some updates to the original answer.

    – Nkosi
    Nov 15 '18 at 0:03













  • Worked perfectly.

    – EmilRR1
    Nov 19 '18 at 22:11

















thank you, giving it a try. Looks awesome though.

– EmilRR1
Nov 14 '18 at 23:04





thank you, giving it a try. Looks awesome though.

– EmilRR1
Nov 14 '18 at 23:04




1




1





@EmilRR1 I made some updates to the original answer.

– Nkosi
Nov 15 '18 at 0:03







@EmilRR1 I made some updates to the original answer.

– Nkosi
Nov 15 '18 at 0:03















Worked perfectly.

– EmilRR1
Nov 19 '18 at 22:11





Worked perfectly.

– EmilRR1
Nov 19 '18 at 22:11




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53309884%2fhow-to-return-custom-message-in-web-api-post-method%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma