gpt4 book ai didi

api - 获取 System.InvalidOperationException : The constraint reference ' int' could not be resolved to a type

转载 作者:行者123 更新时间:2023-12-03 08:41:22 24 4
gpt4 key购买 nike

一切都在 API 中正常工作了数周,然后今天我进行了更改以添加另一个 Controller 方法,但现在所有路由都不起作用。

Controller 定义:

[Route("api/v{version:apiVersion}/Group")]
[ApiController]
public class GroupController : ControllerBase

添加的方法定义:

    /// <summary>
/// Get Members of Group
/// </summary>
/// <param name="groupId">Id of the group to retreive members from</param>
/// <returns>The http status code 204 if sucessful</returns>
[AllowAnonymous]
[HttpGet("Members/{id:int}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public IActionResult Members(int id)
{
var members = _groupLib.GetMembers(id);
if (members == null)
{
return NotFound();
}

return Ok(members);
}

正如您所看到的,有一个约束应用于 id,但该约束已被 Controller 内的其他方法使用。

我什至尝试消除约束,但没有快乐。不需要约束的方法也已停止工作。

这是我的startup.cs

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();
new ConfigDIServices(services);
services.AddAutoMapper(typeof(ObjectMappings));
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();

services.AddDbContext<AppIdentityContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

services.AddIdentity<IdentityUser, IdentityRole>(options =>
{
options.Password.RequiredLength = 8;
options.Password.RequireDigit = false;
options.Password.RequireLowercase = false;
}).AddEntityFrameworkStores<AppIdentityContext>();

services.AddApiVersioning(options =>
{
options.AssumeDefaultVersionWhenUnspecified = true;
options.DefaultApiVersion = new ApiVersion(1, 0);
options.ReportApiVersions = true;
});
services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
});
services.AddSwaggerGen();

// Get AppSettings
var appsettingsSection = Configuration.GetSection("AppSettings");
services.Configure<AppSettings>(appsettingsSection);

var appSettings = appsettingsSection.Get<AppSettings>();
var key = Encoding.ASCII.GetBytes(appSettings.Secret);

services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false

};
});

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, IApiVersionDescriptionProvider provider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseHttpsRedirection();

app.UseSwagger();
// Swagger UI options
app.UseSwaggerUI(options =>
{
foreach (var desc in provider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"/Swagger/{desc.GroupName}/swagger.json", desc.GroupName.ToUpperInvariant());
}
options.RoutePrefix = "";
options.DocExpansion(DocExpansion.None);
});

app.UseRouting();
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
);
app.UseAuthentication();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}

最佳答案

事实证明,参数和约束类型之间以及另一个 Controller 中存在空格。

我有这个

[HttpGet("GetGroupLeaders/{id: int}")] 

当更改为

[HttpGet("GetGroupLeaders/{id:int}")]

一切都开始运转了。我认为这是一个有解决方法的“错误”。应在测试前修剪约束。

关于api - 获取 System.InvalidOperationException : The constraint reference ' int' could not be resolved to a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62667806/

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