Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a function in a Web API using Postman?

I have a function that checks whether a user already exists by looking through the database to see if the email registered has already been used. The function accepts the email address as a parameter, and can be seen below:

[Route("api/[controller]")]
[ApiController]
public class ReportController : ControllerBase
    {
        [HttpPost]
        [Route("UserExists")]
        private bool UserExists(string EmailAddress)
        {
            using var db = new NorthwindContext();
            var user = db.UserLogin.Where(zz => zz.EmailAddress == EmailAddress).FirstOrDefault();

            return user != null;
        }
    }

Now, using PostMan I am trying to pass an email address to check what the function returns, if my understanding is correct, it should be either true or false, considering the function is of type bool.

However, PostMan indicates that the requested resource cannot be found:

Postman 404 Error

My Startup.cs file:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace INF354ReportHomework
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options => options.AddDefaultPolicy(
                builder =>
                {
                    builder.AllowAnyOrigin();
                    builder.AllowAnyHeader();
                    builder.AllowAnyMethod();
                }));
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();
            app.UseCors();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}
like image 468
SeventhWarhawk Avatar asked Apr 02 '26 15:04

SeventhWarhawk


1 Answers

try to change UserExists method from private to public, controllers methods should be public.

like image 96
DvirB Avatar answered Apr 04 '26 07:04

DvirB