How do I solve a “view not found” exception in asp.net core mvc project

前端 未结 16 1944
梦毁少年i
梦毁少年i 2020-12-09 15:53

I\'m trying to create a ASP.NET Core MVC test app running on OSX using VS Code. I\'m getting a \'view not found\' exception when accessing the default Home/index (or any oth

相关标签:
16条回答
  • 2020-12-09 16:00

    Reply to @LukaszDev (dont think I can attach images to a comment) My view is Index.cshtml, typo from my side. See attached screenshot

    My controller is as simple as can be. I get same error for both Index and Welcome

    using Microsoft.AspNetCore.Mvc;
    
    namespace app1 {
    
        [Route("[controller]")]
        public class HomeController : Controller {
    
            // default action, configured in Startup.cs route
            public IActionResult Index() {
                // handles route /home
                return View();
            }
    
            [Route("welcome")]
            public IActionResult WelcomeActionDoesNotHaveToMatchName() {
                // handles router /home/welcome
                return View("Welcome"); 
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-09 16:01

    Visual Code With dotnet version 2.2.300 gave an error message, so not a runtime exception.

    My controller was derived from ControlerBase instead of Controller. So, I had to change ControllerBase to Controller.

    0 讨论(0)
  • 2020-12-09 16:03

    For anyone struggling with this, I had a different problem than all the above. My local version worked fine, but the live production version gave this error. It turned out the Unix filenames were different from the ones showing in Windows.

    Git has a nasty setting that's enabled by default: core.ignorecasegit ( you can check this setting on the commandline with config --get core.ignorecase)

    The solution was to rename the files to something else (e.g. xxx.cs ), commit and push, then rename them back to original with the correct casing/capitalization and commit and push again.

    0 讨论(0)
  • 2020-12-09 16:05

    if you are porting your project from a console app to a web app, you need to change this line in your .csproj file

    from:

    <Project Sdk="Microsoft.NET.Sdk">
    

    to:

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
    0 讨论(0)
  • 2020-12-09 16:06

    In my case after migrating from Core 2.2 to 3.1 my static html file was not being found. In my startup.cs file I had to add app.UseDefaultFiles() just before app.UseStaticFiles() to make it work. So something like this:

    public void ConfigureServices(IServiceCollection services)
    {
       .
       .
       .
       services.AddControllersWithViews();
       .
       .
       .
    }
    
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
       .
       .
       .
       app.UseDefaultFiles();
       app.UseStaticFiles();
       app.UseRouting();
       .
       .
       .
       app.UseEndpoints(endpoints =>
       {
              endpoints.MapControllerRoute(
              name: "default",
              pattern: "{controller=Home}/{action=Index}/{id?}");
       });
    }
    
    0 讨论(0)
  • 2020-12-09 16:07

    I found this missing piece. I ended up creating a ASP.NET Core project in VS2015 and then compare for differences. It turns out I was missing .UseContentRoot(Directory.GetCurrentDirectory()) from WebHostBuilder in main.

    After adding this:

    public static void Main(string[] args)
    {
        new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseStartup<Startup>()
            .Build()
            .Run();
    }
    

    I then got an exception regarding missing preserveCompilationContext. Once added in project.json my view shows correct.

    "buildOptions": {
        "preserveCompilationContext": true,
        "emitEntryPoint": true
    },
    
    0 讨论(0)
提交回复
热议问题