我跟着几个教程走了一遍,在工作中这个能够正常运行,但是出现了一个问题,即无法显示用户界面,但是Swagger Json已经被创建了。最后一个我看的教程在这里。
我的设置如下:
Nuget包: Swashbuckle.AspNetCore(1.0.0)
ConfigureServices
方法:
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1",
new Info
{
Title = "MediatR Example",
Version = "v1",
Description = "Trying out the MediatR library to simplify Request and Response logic.",
TermsOfService = "WTFPL",
Contact = new Contact
{
Email = "",
Name = "",
Url = "https://github.com/CubicleJockey/MediatR-Playground"
}
}
);
var xmlDocFile = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, @"MediatR-Messages.Api.xml");
options.IncludeXmlComments(xmlDocFile);
options.DescribeAllEnumsAsStrings();
});
配置
方法:
app.UseMvcWithDefaultRoute();
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS etc.), specifying the Swagger JSON endpoint
app.UseSwaggerUI(config =>
{
config.SwaggerEndpoint("/swagger/v1/swagger.json", "V1 Docs");
});
launchSettings.json
:
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger/",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
运行并访问Swagger JSON URL 会生成相应的 JSON:
{
"swagger":"2.0",
"info":{
"version":"v1",
"title":"MediatR Example",
"description":"Trying out the MediatR library to simplify Request and Response logic.",
"termsOfService":"WTFPL",
"contact":{
"name":"André Davis",
"url":"https://github.com/CubicleJockey/MediatR-Playground",
"email":"davis.andre@gmail.com"
}
},
"basePath":"/",
"paths":{
"/api/Addition":{
"get":{
"tags":[
"Addition"
],
"summary":"Get Methods that takes two numbers and gets the sum.",
"operationId":"ApiAdditionGet",
"consumes":[
],
"produces":[
"text/plain",
"application/json",
"text/json"
],
"parameters":[
{
"name":"left",
"in":"query",
"description":"Left hand side of the equation.",
"required":false,
"type":"integer",
"format":"int32"
},
{
"name":"right",
"in":"query",
"description":"Right hand side of the equation.",
"required":false,
"type":"integer",
"format":"int32"
}
],
"responses":{
"200":{
"description":"Success",
"schema":{
"$ref":"#/definitions/Task[AdditionResponse]"
}
}
}
}
}
},
"definitions":{
"Task[AdditionResponse]":{
"type":"object",
"properties":{
"result":{
"$ref":"#/definitions/AdditionResponse",
"readOnly":true
},
"id":{
"format":"int32",
"type":"integer",
"readOnly":true
},
"exception":{
"type":"object",
"readOnly":true
},
"status":{
"enum":[
"Created",
"WaitingForActivation",
"WaitingToRun",
"Running",
"WaitingForChildrenToComplete",
"RanToCompletion",
"Canceled",
"Faulted"
],
"type":"string",
"readOnly":true
},
"isCanceled":{
"type":"boolean",
"readOnly":true
},
"isCompleted":{
"type":"boolean",
"readOnly":true
},
"isCompletedSuccessfully":{
"type":"boolean",
"readOnly":true
},
"creationOptions":{
"enum":[
"None",
"PreferFairness",
"LongRunning",
"AttachedToParent",
"DenyChildAttach",
"HideScheduler",
"RunContinuationsAsynchronously"
],
"type":"string",
"readOnly":true
},
"asyncState":{
"type":"object",
"readOnly":true
},
"isFaulted":{
"type":"boolean",
"readOnly":true
}
}
},
"AdditionResponse":{
"type":"object",
"properties":{
"answer":{
"format":"int32",
"type":"integer",
"readOnly":true
},
"equation":{
"type":"string",
"readOnly":true
}
}
}
},
"securityDefinitions":{
}
}
当访问默认的Swagger UI url时,我遇到了404错误。尝试了几个变化:
- localhost:64881/swagger/
- localhost:64881/swagger/ui
- localhost:64881/swagger/index.html
- localhost:64881/swagger/ui/index.html
以上所有都返回404。这些在不同的版本中曾经起作用过。我错过了什么?
完整的源代码可以在我的GitHub 这里 找到。这是一个针对此问题的分支,因此代码与我的请求相匹配。