MVC Areas - View not found

2019-02-04 15:59发布

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 dispatch controller I have setup that routes default incoming requests to the Home Controller in my area.

This controller has one method as follows:-

public ActionResult Index(string id)
    {
        return RedirectToAction("Index", "Home", new {area = "xyz"});
    }   

I also have a default route setup to use this controller as follows:-

routes.MapRoute(
            "Default",                                              // Default route
            "{controller}/{action}/{id}",
            new { controller = "Dispatch", action = "Index", id = UrlParameter.Optional }
        );   

Any default requests to my site are appropriately routed to the relevant area. The Area's "RegisterArea" method has a single route:-

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

My area has multiple controllers with a lot of views. Any call to a specific view in these controller methods like "return View("blah"); renders the correct view. However whenever I try and return a view along with a model object passed in as a parameter I get the following error:-

Server Error in '/DeveloperPortal' Application.
The view 'blah' or its master was not found. The following locations were searched:
~/Views/Profile/blah.aspx
~/Views/Profile/blah.ascx
~/Views/Shared/blah.aspx
~/Views/Shared/blah.ascx 

It looks like whenever a model object is passed in as a param. to the "View()" method [e.g. return View("blah",obj) ] it searches for the view in the root of the project instead of in the area specific view folder.

What am I missing here ?

Thanks in advance.

7条回答
对你真心纯属浪费
2楼-- · 2019-02-04 16:13

I just had the same problem and solved it by setting the ascx's 'Build Action' property to 'Embedded Resource'.

查看更多
贪生不怕死
3楼-- · 2019-02-04 16:13

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" }
);
查看更多
唯我独甜
4楼-- · 2019-02-04 16:19

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...

查看更多
戒情不戒烟
5楼-- · 2019-02-04 16:30

Solved ! A couple of my "RedirectToAction" calls were not specifying the area name explicitly in the routeobject collection parameter of that method. Weird though, that that is required even though the controllers Redirecting are all in the same area. Also, the HtmlActionLinks work fine when I don't specify the new {area="blah"} in its routeobject collection, so I wonder why the controller action calls to RedirectToAction() need that even though both the calling and the called controller actions are all within the same area.

查看更多
Summer. ? 凉城
6楼-- · 2019-02-04 16:30

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.

查看更多
混吃等死
7楼-- · 2019-02-04 16:30

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 }
        );
    }
查看更多
登录 后发表回答