我设置了断点,然后按照流程进行操作。正在实例化控制器,并且正在验证参数(我设置了一些数据注释),但是控制器方法本身没有被调用。
我认为问题在于路由,但我不确定它可能会被破坏。我还没有建立任何全球路由;一切都是通过属性完成的。 Swagger文档也正在生成,并记录控制器端点。
事实上,表面上的一切似乎都行得通。发送请求,我将返回200 OK
。发送未通过声明性验证的请求,我将返回BadRequest
。
这是控制器:
[Route("licensing")]
public class LicenseController
: Controller
{
[HttpPut]
[Route("create-license/{licenseKey}")]
public async Task<LicenseDetails> CreateLicenseAsync(string licenseKey, [FromBody]CreateLicenseRequest license)
{
DoSomething(licenseKey); //A breakpoint here will never be hit
}
}
请求如下所示:
{
"Username": "test@example.com",
"LicenseKey": "license-key-string",
"ProductName": "SomeProduct",
"ProductVersion": "2017.5",
"ActivationLimit": 2147483647,
"UtcExpiration": "9998-12-31T23:59:59.9999999"
}
PUT
会违反以下网址:
https://api.example.com/licensing/create-license/license-key-string
我在标题中使用基本身份验证,它执行我编写的基本身份验证过滤器,并且一切正常。 (我介入;如果发生故障,则返回NotAuthorized
。)
这里的大部分作品都是根源:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("hosting.json", optional: false, reloadOnChange: true)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public void ConfigureServices()
{
//...non-ASP.NET stuff snipped
services.AddMvc(options =>
{
options.Filters.Add(new ValidateModelStateAttribute());
options.Filters.Add(new BasicAuthenticationAttribute(authorizationManager));
options.Filters.Add(new ApiExceptionFilter());
options.OutputFormatters.Clear();
options.OutputFormatters.Add(new JsonOutputFormatter(jsonSerializerSettings, ArrayPool<char>.Shared));
})
.AddJsonOptions(setupAction: options =>
{
//...
});
services.AddSingleton<IControllerActivator>(new LicenseKeyControllerActivator(keyService));
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "Licensing API", Version = "v1" });
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseMvc();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Licensing API v1");
});
}