【发布时间】: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