MDC*_*MDC 2 api asp.net-core-3.1
API 中的一切都已正常工作数周,然后今天我进行了更改以添加另一个控制器方法,但现在所有路由都不起作用。
控制器定义:
[Route("api/v{version:apiVersion}/Group")]
[ApiController]
public class GroupController : ControllerBase
Run Code Online (Sandbox Code Playgroud)
方法 def 添加:
/// <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);
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,有一个约束应用于 id 但已被控制器内的其他方法使用。
我什至尝试消除限制,但没有快乐。不需要约束的方法也已停止工作。
这是我的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();
});
}
}
Run Code Online (Sandbox Code Playgroud)
事实证明,参数和约束类型之间以及另一个控制器中存在空格。
我有这个
[HttpGet("GetGroupLeaders/{id: int}")]
Run Code Online (Sandbox Code Playgroud)
当更改为
[HttpGet("GetGroupLeaders/{id:int}")]
Run Code Online (Sandbox Code Playgroud)
一切都开始运转了。我认为这是一个有解决方法的“错误”。测试前应修剪约束。