我试图使用F#与.NET Core 2.1创建一个简单的CRUD应用程序,但没有一个控制器没有注册.我看不到Swagger中的控制器和控制器本身都没有启动.
我感谢任何帮助或提示.
Startup.fs
namespace SimpleCms
type Startup private () =
let mutable configuration : IConfigurationRoot = null
member this.Configuration
with get () = configuration
and private set (value) = configuration <- value
new(env : IHostingEnvironment) as this =
Startup()
then
let builder =
ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional = false, reloadOnChange = true)
.AddJsonFile(sprintf "appsettings.%s.json" env.EnvironmentName, optional = true)
.AddEnvironmentVariables()
this.Configuration <- builder.Build()
// This method gets called by the runtime. Use this method to add services to the container.
member this.ConfigureServices(services : IServiceCollection) =
services.AddLogging() |> ignore
services.AddSwaggerGen(fun x ->
x.SwaggerDoc("v1", new Info(Title = "SimpleCms", Version = "v1")) |> ignore)
|> ignore
services.AddMvcCore() |> ignore
services.AddMvc() |> ignore
let container =
new Container(fun opt ->
opt.Scan(fun x ->
x.AssemblyContainingType(typeof<Startup>)
x.Assembly("Dal")
x.Assembly("Logic")
x.WithDefaultConventions() |> ignore)
opt.For<LiteDatabase>().Use(new LiteDatabase("Filename=database.db")) |> ignore
opt.Populate(services) |> ignore)
container.GetInstance<IServiceProvider>()
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
member this.Configure(app : IApplicationBuilder, env : IHostingEnvironment) =
app.UseSwagger() |> ignore
app.UseSwaggerUI(fun x ->
x.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1") |> ignore) |> ignore
app.UseMvc(fun x ->
x.MapRoute("default", "{controller=Home}/{action=Index}") |> ignore) |> ignore
Run Code Online (Sandbox Code Playgroud)
HomeController.fs
namespace SimpleCms.Controllers
open Microsoft.AspNetCore.Mvc
[<Route("")>]
type HomeController() =
inherit Controller()
[<Route("")>]
[<HttpGet>]
member this.Index() =
Ok("Hello World!")
Run Code Online (Sandbox Code Playgroud)
完整的代码
你的问题是IPostController接口问题。删除它,Swagger 将拾取PostController.
例如:
[<Route("api/[controller]")>]
[<ApiController>]
type PostController(logic : IPostLogic) =
inherit Controller()
member this.logic = logic
[<Route("")>]
[<HttpGet>]
member this.GetAll() =
this.logic.GetAll()
//etc
Run Code Online (Sandbox Code Playgroud)
这使得 Swagger 展示了这一点:
旁注:对于 ASP.NET Core 2.1 及更高版本,您不应指定Microsoft.AspNetCore.All包的版本,您的fsproj文件应包含以下内容:
<PackageReference Include="Microsoft.AspNetCore.All" />
Run Code Online (Sandbox Code Playgroud)
这又意味着您应该使用 2.1.1 版本Microsoft.Extensions.Logging.Debug。
最后,您使用的是非常旧的版本Swashbuckle.AspNetCore。我建议你也升级一下。
| 归档时间: |
|
| 查看次数: |
278 次 |
| 最近记录: |