MVC Areas - View not found

前端 未结 8 1439
盖世英雄少女心
盖世英雄少女心 2020-12-15 08:35

I have a project that is using MVC areas. The area has the entire project in it while the main \"Views/Controllers/Models\" folders outside the Areas are empty barring a dis

相关标签:
8条回答
  • 2020-12-15 08:48

    Try this code. Do changes in Area Registration File...

    context.MapRoute(
        "YourRouteName",   // Route name //
        "MyAreaName/MyController/{action}",   // URL with parameters //
        new { 
            controller = "MyControllerName", 
            action = "MyActionName", meetId =  UrlParameter.Optional
         },   // Parameter defaults
        new[] { "Your Namespace name" }
    );
    
    0 讨论(0)
  • 2020-12-15 08:56

    For those who are looking for .net core solution please use

     app.UseMvc(routes =>
        {
          routes.MapRoute(
            name : "areas",
            template : "{area:exists}/{controller=Home}/{action=Index}/{id?}"
          );
        });`
    

    If you have some code in main project and some code in areas use the following code.

    app.UseMvc(routes => {
                routes.MapRoute(
                    name: "areas",
                    template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
    
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
    

    Note make sure you have areas defined in your controller

     [Area("Test")]
    public class TestController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
    

    }

    0 讨论(0)
  • If you use instead of

    context.MapRoute(
            "xyz_default",
            "xyz/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
    

    use

    context.MapRoute(
            "xyz_default",
            "{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
    

    in your

    xyzAreaRegistration.cs

    then you don't need to explicitly specify your area in any link...

    0 讨论(0)
  • 2020-12-15 09:00

    Check the generated code at MyAreaAreaRegistration.cs and make sure that the controller parameter is set to your default controller, otherwise the controller will be called bot for some reason ASP.NET MVC won't search for the views at the area folder

    public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "SomeArea_default",
                "SomeArea/{controller}/{action}/{id}",
                new { controller = "SomeController", action = "Index", id = UrlParameter.Optional }
            );
        }
    
    0 讨论(0)
  • 2020-12-15 09:03

    Add the RouteArea attribute on the Controller class so MVC knows to use the "XYZ" Area for the views (and then you can set the AreaPrefix to empty string so routes do not need to start with "XYZ").

    [RouteArea("Xyz", AreaPrefix = "")]
    public class XyzController : Controller   
    {
    ...
    }
    
    0 讨论(0)
  • 2020-12-15 09:05

    If this is a routing problem, you can fix it by registering your area routes first. This causes the routing engine to try matching one of the area routes, before matching a root route:

    AreaRegistration.RegisterAllAreas();
    RegisterRoutes(RouteTable.Routes);
    

    If I force an error by renaming one of my views folders in my areas application, I get a different error than yours:

    The view 'Index' or its master was not found. The following locations 
      were searched:
    
    ~/Areas/xyz/Views/Document/Index.aspx
    ~/Areas/xyz/Views/Document/Index.ascx
    ~/Areas/xyz/Views/Shared/Index.aspx
    ~/Areas/xyz/Views/Shared/Index.ascx
    
    ...and then the usual root view folders.. 

    ..which is the pattern of subdirectories it would search if it thought it was in an area.

    0 讨论(0)
提交回复
热议问题