I've been trying to create a very simple MVC project in F#.
Here's a brief explanation of what I've done and I'd appreciate any help with it.
- I created a new "ASP .NET Core Web Application" using VS 2019 template for F#.
- Then I selected "ASP.NET Core Empty", ASP.NET Core 5.0.
- I added a few files to the project in order to make the structure look as follows:
/ Root
| appsettings.json
| Views
| Home
| Index.cshtml // added as "Content", "Do not copy"
HomeController.fs
Startup.fs
Program.fs
The controller is very simple:
type HomeController() =
inherit Controller()
this.Index() =
this.View()
Then, when I launch my app, I get error:
InvalidOperationException: The view 'Index' was not found. The following locations were searched:
/Views/Home/Index.cshtml
/Views/Shared/Index.cshtml
So even though my cshtml file is there, it can't be found by the runtime.
I made sure "UseStaticFiles" is enabled in the Startup class:
type Startup() =
member __.ConfigureServices(services) : unit =
services.AddControllersWithViews() |> ignore
member __.Configure(app, env) : unit =
// env.ContentRootFileProvider.Root is set to my Root folder
app.UseStaticFiles() |> ignore
app.UseRouting() |> ignore
app.UseAuthorization() |> ignore
app.UseEndpoints(fun endpoints ->
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}") |> ignore
) |> ignore
Going further
I tried walking the problem around and read the content of cshtml manually and return it as string
type HomeController() =
inherit Controller()
member this.ManualIndex() =
let html = System.IO.File.ReadAllText("/Views/Index.cshtml")
this.Content(html, "text/html")
and it worked, but from the HTML I still can't refer to any other static files, like css or js. I also tried creating wwwroot directory and putting my static content there, but then didn't help either.
question from:
https://stackoverflow.com/questions/65943337/asp-net-core-problem-with-views-and-static-files-f 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…