我已经安装了Swashbuckle v5.3.2,Swashbuckle.OData v2.18.3但是Swagger文档仍然没有显示我的OData控制器。我的API控制器看起来很好。我做错了什么?
这是我的控制器类:
[LogActionFilter, LogExceptionFilter]
public class UsuarioODataController : ODataController {
private static FluentValidation.AbstractValidator<UsuarioMobile> Validator = new UsuarioMobileValidator();
private IUsuarioMobileService _service;
public UsuarioODataController(IUsuarioMobileService service)
{
_service = service;
}
[EnableQuery]
public async Task<IQueryable<UsuarioMobile>> Get()
{
var result = _service.Queryable();
return result;
}
[EnableQuery]
public async Task<SingleResult<UsuarioMobile>> Get([FromODataUri] int key)
{
IQueryable<UsuarioMobile> result = _service.Queryable().Where(x => x.Id == key);
return SingleResult.Create(result);
}
public async Task<IHttpActionResult> Put([FromODataUri] int key, UsuarioMobile model)
{
try
{
model.Id = key;
var validationResult = Validator.Validate(model);
if (validationResult.IsValid)
{
return Ok(await _service.Update(model));
}
return BadRequest(validationResult.Errors.ToJson());
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
public async Task<IHttpActionResult> Post(UsuarioMobile model)
{
try
{
var validationResult = Validator.Validate(model);
if (validationResult.IsValid)
{
return Ok(await _service.Add(model));
}
return BadRequest(validationResult.Errors.ToJson());
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
public async Task<IHttpActionResult> Delete([FromODataUri] int key)
{
try
{
return Ok(await _service.DeleteAsync(key));
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
}
启动课程:
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
var container = InjectorConfig.InjectorInitialize();
ConfigureAuth(app);
ConfigurationApp(app, container);
}
private void ConfigurationApp(IAppBuilder app, Container container)
{
var config = new HttpConfiguration
{
DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container),
IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always
};
//Swagger
config
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "MTM.MeuEinstein.IntegratorHIAE");
c.IncludeXmlComments(SwaggerConfig.GetXmlCommentsPath());
c.IncludeXmlComments(SwaggerConfig.GetXmlCommentsPathForModels());
c.CustomProvider(defaultProvider => new ODataSwaggerProvider(defaultProvider, c, config));
})
.EnableSwaggerUi(c =>
{ });
config.MapHttpAttributeRoutes();
WebApiConfig.Register(config);
RouteConfig.RegisterRoutes(RouteTable.Routes);
var jsonFormatter = config.Formatters.JsonFormatter;
var jSettings = new JsonSerializerSettings
{
Formatting = Formatting.None,
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
DateFormatString = "dd/MM/yyyy HH:mm:ss",
//TODO: Necessário importar
//Culture = CultureInfo.GetCultureInfo(SplCulture.GetCurrentCulture()),
NullValueHandling = NullValueHandling.Ignore,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
//config.MessageHandlers.Add(new WebApiHandler());
//config.MessageHandlers.Add(new SplCookieHandler());
jsonFormatter.SerializerSettings = jSettings;
config.Formatters.Clear();
config.Formatters.Insert(0, jsonFormatter);
app.UseCors(CorsOptions.AllowAll);
app.UseWebApi(config);
}
}
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
//config.SuppressDefaultHostAuthentication();
//config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
//config.MapHttpAttributeRoutes();
// OData routes
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<UsuarioMobile>("UsuarioOData");
config.Routes.MapODataServiceRoute("ODataApi", "api", builder.GetEdmModel());
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}