Beginning Azure Static Web Apps by 2022

Beginning Azure Static Web Apps by 2022

Author:2022
Language: eng
Format: epub


Chapter 6 retrieving Blog Data

Retrieving a Single, Full Blog Post

Now that we can return a list of summaries, we also need to return a single blog post to display in full. For that, we are going to reuse the same Azure Function file but add a new method which will have a modified route.

1. Copy the code from Code 6-12 underneath the existing function.

Code 6-12. Get Single Blog Post Function

[FunctionName($"{nameof(BlogPosts)}_GetId")]

public static IActionResult GetBlogPost(

[HttpTrigger(AuthorizationLevel.Anonymous, "get",

Route = "blogposts/{author}/{id}")]

HttpRequest req,

[CosmosDB("SwaBlog", "BlogContainer",

Connection = "CosmosDbConnectionString",

SqlQuery = @"SELECT

c.id,

c.Title,

c.Author,

c.PublishedDate,

c.BlogPostMarkdown,

c.Status,

c.Tags

FROM c

WHERE c.id = {id} and c.Author={author}")]

IEnumerable<BlogPost> blogposts,

ILogger log)

{

if (blogposts.ToArray().Length == 0)

{

return new NotFoundResult();

}

return new OkObjectResult(blogposts.First());

}

138



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.