【问题标题】:How to call a function in a Web API using Postman?如何使用 Postman 在 Web API 中调用函数?
【发布时间】:2021-08-25 09:55:23
【问题描述】:

我有一个功能,通过查看数据库来检查用户是否已经存在,以查看注册的电子邮件是否已被使用。该函数接受电子邮件地址作为参数,如下所示:

[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;
        }
    }

现在,我正在使用 PostMan 传递一个电子邮件地址来检查函数返回的内容,如果我的理解是正确的,它应该是 true 或 false,考虑到函数的类型是 bool。

但是,PostMan 提示找不到请求的资源:

我的 Startup.cs 文件:

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();
            });
        }
    }
}

【问题讨论】:

  • 在查询字符串中传递是否有效?
  • 你的意思是private
  • 请在问题中添加您的启动文件
  • 无关:我建议使用return db.UserLogin.Any(zz => zz.EmailAddress == EmailAddress);
  • 我支持private 问题,您尝试过[FromBody] string EmailAddress 吗?

标签: c# asp.net asp.net-mvc asp.net-web-api postman


【解决方案1】:

您可以像这样向您的请求添加参数:

您还应该将方法的保护等级从私有更改为公共。

【讨论】:

    【解决方案2】:

    尝试将 UserExists 方法从私有更改为公共, 控制器方法应该是公开的。

    【讨论】:

    • 谢谢,也谢谢所有提到它是私人而不是公开的事实的人。这行得通。
    猜你喜欢
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 2019-06-02
    • 2019-04-27
    • 1970-01-01
    • 2022-12-28
    • 2019-12-18
    • 2018-11-12
    相关资源
    最近更新 更多