Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

How Do I Block Data from Rendering in an API Controller?

$
0
0

I have an api controller that gets data from a service that returns more data than I want rendered in the json output, but I also need to access the same data from Razor Pages. How do I block that output at the controller level?

Normally I would just omit the data from the get task that the service performs, so that it would render as null in json output, but if I do that I can't access it from server side code in a Razor Page. The api controller is used because json is needed for an infinite scroll plugin. Otherwise I would not even need a controller.

Is there perhaps a quick line of code that can be added to a Controller that tells it to omit a specific field from the json output?

Here is the task redacted for efficiency from the Service file:

public async Task<Posts[]> GetPosts()
        { 
            var posts = await _context.Posts.Where(post => post.Active == true)
                            .Select(p => new Posts { 
                            Postid = p.Postid,
                            Title = p.Title,
                            Description = p.Description,
                            Userid = p.Userid 
                            })
                        .ToArrayAsync();
            return posts;
        }

A second task then paginates the results of the first one

public async Task<List<Posts>> GetPaginatedResult(int currentPage, int pageSize)
        {
            var data = await GetPosts();
            return data.OrderByDescending(d => d.Postid).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
        }

The controller is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PostAlmostAnything.Data;
using PostAlmostAnything.Models;
using PostAlmostAnything.SiteServices;
using PostAlmostAnything.AppCode;

namespace PostAlmostAnything.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PaginatedPostsController : ControllerBase
    {
        private readonly ApplicationDbContext _context;

        public PaginatedPostsController(PostsService postService)
        {
            PostService = postService;
        }
        public PostsService PostService { get; }
 [HttpGet("{id:int}/{sid:int}")]
        public async Task<List<Posts>> GetPaginatedPostsAsync(int id, int sid)
        {
            int CurrentPage = id;
            int PageSize = sid;
            return await PostService.GetPaginatedResult(CurrentPage, PageSize);
        }

        
    }
}

Obviously there are reasons not to render the Userid field in the json output, but I also need to access the Userid in razor pages that need to get the UserName based on that id. I am using the default Identity tables in my database and those tables by default have no foreign key relationships with one another, so when they are scaffolded automatically they do not create navigation classes. Identity also stores the UserId as a string instead of a unique identifier like the old AspnetMembership provider did, so I'm not really sure how to go about creating a foreign key relationship between string values. I seem to recall trying to do that once and running into some kind of error message about SQL Server not supporting such relationships with strings. As a result I have another task called GetUserNambeById that I call from razor pages to populate UserName fields. This requires GetPosts to return the UserId so that it can be passed to GetUserNameById.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>