【发布时间】:2021-05-21 15:26:55
【问题描述】:
我正在尝试开始一个 ASP.NET F# 项目,遵循 mvc 类型模式。
我正在使用 Rider IDE,因为我正在运行 linux。 Rider IDE 具有开箱即用的 ASP.NET MVC 模板,因此要开始这个项目,我选择了 ASP.NET Core Web 应用程序模板。
我现在有一个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。
所以它抱怨我没有在配置方法中调用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 中有这样的电话。
【问题讨论】:
-
你似乎忘记了括号:UseRouting() 和 UseAuthorization()
标签: .net asp.net-mvc f#