簡體   English   中英

為 f# asp.net 核心項目配置啟動文件給出奇怪的錯誤

[英]Configuring Startup file for f# asp.net core project is giving strange errors

我正在嘗試按照 mvc 類型模式開始 ASP.NET F# 項目。

我正在使用騎手 IDE,因為我正在運行 linux。 The rider IDE has ASP.NET MVC templates, out of the box, so to get started with this project, I choose the ASP.NET Core web application template.

我現在有一個Program.fs文件和一個Startup.fs ,我可以運行該項目並在瀏覽器中查看一個小小的 helloworld-page。

但現在我想設置路由,以便我可以路由到控制器。

為此,我查看了 C# MVC 項目的骨架,並嘗試模擬Startup文件。

我的啟動文件現在看起來像這樣:

namespace WebApplication1

open Microsoft.AspNetCore.Builder

open System
open Microsoft.AspNetCore.Builder
open Microsoft.AspNetCore.Hosting
open Microsoft.AspNetCore.Http
open Microsoft.Extensions.Configuration
open Microsoft.Extensions.DependencyInjection
open Microsoft.Extensions.Hosting

type Startup() =
    

  
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    member _.ConfigureServices(services: IServiceCollection) =
    services.AddAuthorization()

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    member _.Configure(app: IApplicationBuilder, env: IWebHostEnvironment) =
        if env.IsDevelopment() then
            app.UseDeveloperExceptionPage() |> ignore
                     
        app.UseHttpsRedirection() |> ignore
        app.UseStaticFiles() |> ignore
        app.UseRouting() |> ignore
        app.UseAuthorization)() |> ignore
    
        app.UseEndpoints(fun endpoints ->
            endpoints.MapControllerRoute("default","{controller=Home}/{action=Index}/{id?}" ) |> ignore
            ) |> ignore

現在,當我運行它時,我收到以下錯誤:

未處理的異常。 System.InvalidOperationException:EndpointRoutingMiddleware 與 EndpointMiddleware 設置的端點匹配,因此必須在 EndpointMiddleware 之前添加到請求執行管道中。 請通過在應用程序啟動代碼中對“Configure(...)”的調用中調用“IApplicationBuilder.UseRouting”來添加 EndpointRoutingMiddleware。

所以它抱怨我沒有在 configure 方法中調用IApplicationBuilder.UseRouting

這對我來說很奇怪,因為我實際上正在打這樣的電話。

有誰知道我如何重寫它以啟用到控制器的路由?

編輯

有人向我指出我缺少括號。 我修復了這個問題,然后編譯器告訴我我還缺少ConfigureServices內部的調用services.AddAuthorization() 我現在修復了這個問題,上面的代碼與我正在運行的代碼相對應。

現在當我運行它時,我得到:

Unhandled exception. System.InvalidOperationException: Unable to find the required services. Please add all the required services by calling
  'IServiceCollection.AddAuthorization' inside the call to      'ConfigureServices(...)' in the application startup code.

這對我來說不太有意義,因為我在ConfigureServices中有這樣的調用。

一個問題是您的ConfigureServices根本沒有被調用,因為它的簽名錯誤。 試試這個:

member _.ConfigureServices(services: IServiceCollection) =
    services.AddAuthorization() |> ignore

不幸的是,在這種情況下, ignore是至關重要的。 這個 SO question本質上是一回事。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM