Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC doesn't find view in area

I request an action which is inside an area. Route is correct so debugger goes into the action. If I access RouteData property in the action, I see controller, action and area names (so area key is set). But during View rendering I get an exception telling that view can not be find (it searches only among root-level views, not inside area). But if I explicitly specify View name, it works.

So the question is how to make it work implicitly?

Update1 Here is a screenshot with RouteData in Watches in my project structure: enter image description here

Update2 Here is the text of the exception:

The view 'Battles' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/AdminBattles/Battles.aspx ~/Views/AdminBattles/Battles.ascx ~/Views/Shared/Battles.aspx ~/Views/Shared/Battles.ascx ~/Views/AdminBattles/Battles.cshtml ~/Views/AdminBattles/Battles.vbhtml ~/Views/Shared/Battles.cshtml ~/Views/Shared/Battles.vbhtml

like image 655
SiberianGuy Avatar asked Jul 29 '11 11:07

SiberianGuy


3 Answers

Sorry, it was my fault. I registered area routes calling context.Routes.MapRoute() instead of context.MapRoute() method.

like image 67
SiberianGuy Avatar answered Nov 20 '22 18:11

SiberianGuy


Idsa,

You may find that a similar question that I had today (which I figured out) may help:

goin back to my 'routes' - issue with partialviews and areas??

basically, in your views web.config file(s), reference the namespaces that directly relate to either the root or the areas portions of the site.

worked for me in a very similar error scenario.

[edit] - re the custom viewengine. just add the following class to your Models folder (with your own custom paths of course):

using System.Linq;
using System.Web.Mvc;

namespace ABC.Web.Site.Areas.Administration.Models
{
    public class ABCViewEngine : WebFormViewEngine
    {
        private static readonly string[] NewPartialViewFormats = 
            new[] {
                    "~/Areas/Administration/Views/Shared/Full/{0}.aspx",
                    "~/Areas/Administration/Views/Shared/Partial/{0}.ascx"
                  };

        public ABCViewEngine()
        {
            base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray();
        }
    }
}

then add the following to your global.asax (Application_Start()):

ViewEngines.Engines.Add(new ABCViewEngine());

good luck..

like image 44
jim tollan Avatar answered Nov 20 '22 18:11

jim tollan


Have you created a folder with the same name as your controller (without the Controller bit) underneath Views and put your view in there?

So, not:

Areas
    AreaName
        Views
            MyView.aspx

but...

Areas
    AreaName
        Views
            ControllerNameWithoutControllerOnTheEnd
                MyView.aspx
like image 2
Steve Morgan Avatar answered Nov 20 '22 17:11

Steve Morgan