gpt4 book ai didi

c# - 如何使用 Postman 在 Web API 中调用函数?

转载 作者:行者123 更新时间:2023-12-05 01:59:23 25 4
gpt4 key购买 nike

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

[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 传递一个电子邮件地址来检查函数返回的内容,如果我的理解是正确的,考虑到函数的类型是 bool,它应该是 true 或 false。

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

Postman 404 Error

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

最佳答案

尝试将 UserExists 方法从私有(private)更改为公共(public), Controller 方法应该是公开的。

关于c# - 如何使用 Postman 在 Web API 中调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67886454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com