24

I have an Azure Function 2.x that reside on a static class that looks like this

[FunctionName("Register")]
public static async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post")]HttpRequest req, ILogger log)
{
    MyTypeClass defReturn = new MyTypeClass();
    HttpStatusCode defCode = HttpStatusCode.BadRequest;

    /*
    * Logics that might or might not changes
    * defReturn and defCode value
    */

    return StatusCode((int) defCode, JsonConvert.SerializeObject(defReturn))
}

How can i achieve the return StatusCode((int) defCode, JsonConvert.SerializeObject(defReturn)) part ? is there any such method or equivalent in Azure Functions 2.x ?

in Azure Functions 1.x i can do the equivalent with req.CreateResponse(defCode, defReturn) where req is HttpRequestMessage , but i'm trying to stick with 2.x template/standard

Additional explanation : The said Code should return HTTP 400 Bad Request with the defReturn as it's response body to the client. But when i change the defCode to HttpStatusCode.Accepted, it should return HTTP 202 Accepted with the same response body. How can i achieve this ?

Additional explanation#2 : (If i remember correctly) in ASP.NET Core 1.x i can exactly do like that, returning IActionResult by calling a static method StatusCode not StatusCodes (which is a static class that contains HTTP codes constants

Thank you

Tommy Aria Pradana
  • 574
  • 1
  • 4
  • 18

2 Answers2

43

Quite late reply, but I was stumbling into the same problem today, so maybe this is helpful for other searchers

Option 1: Default Codes

This is stated in detail on the blog Here

Some codes like 200 and 400 are predefined and can be used by

return new OkObjectResult("Your message"); // 200
return new BadRequestObjectResult("Your error message"); // 400

These functions are not available for every known Status Codes but some of the most frequent.

Option 2: Manual setting Code

If you need specific codes, that are not provided by default, you can use the base classes and create them yourself.

To achieve the Teapot Response for example, you can just use

using Microsoft.AspNetCore.Http;

var result = new ObjectResult("Your message");
result.StatusCode = StatusCodes.Status418ImATeapot;
return result;

In this example, the Statuscode is used from the StatusCodes class, but you can use enter other codes as well (usually, just stick to these codes)

Also, the ObjectResult class offers additional formatting options, if needed.

Viktor
  • 679
  • 6
  • 21
auX
  • 860
  • 9
  • 19
-1

You can create a model class in which you can define two properties, i.e. one form your status code and one for you Json object and later on you can return the complete model. Code sample would be like below:

public static class QueueTriggerTableOutput
{
    [FunctionName("QueueTriggerTableOutput")]
    [return: Table("outTable", Connection = "MY_TABLE_STORAGE_ACCT_APP_SETTING")]
    public static Person Run(
        [QueueTrigger("myqueue-items", Connection = "MY_STORAGE_ACCT_APP_SETTING")]JObject order,
        ILogger log)
    {
        return new Person() {
                PartitionKey = "Orders",
                RowKey = Guid.NewGuid().ToString(),
                Name = order["Name"].ToString(),
                MobileNumber = order["MobileNumber"].ToString() };
    }
}

public class Person
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
    public string Name { get; set; }
    public string MobileNumber { get; set; }
}

on the receiving front, you can catch both the property.

P.S.- you have to change the return type of your function.

Hope it helps.

Mohit Verma
  • 5,140
  • 2
  • 12
  • 27
  • 2
    Hi @MohitVerma-MSFT ,Thank you for your answer. I never thought to change the return type instead, but may i know, regardless the return type, how can i change or configure the HTTP Status Code for the response ? Thanks – Tommy Aria Pradana Feb 25 '19 at 10:25