8

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

SiberianGuy
  • 24,674
  • 56
  • 152
  • 266

4 Answers4

6

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

Greg B
  • 14,597
  • 18
  • 87
  • 141
SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
3

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

Community
  • 1
  • 1
jim tollan
  • 22,305
  • 4
  • 49
  • 63
  • Jim, Great Answer. I've adapted your answer to be a little more dynamic: http://stackoverflow.com/questions/4973000/adding-sub-directory-to-view-shared-folder-in-asp-net-mvc-and-calling-the-view/23018523#23018523 – mcfea Apr 11 '14 at 17:09
2

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
Steve Morgan
  • 12,978
  • 2
  • 40
  • 49
0

Searches tend to take people here, so I'd like to shard another related item.

I've had issues where the view won't be returned due to an unexpected miss-match of the overloaded method I meant to be calling, and the actual method signature match.

For example, if you have Id as a string, then View("Something",id) will match the overload expecting a string ViewName and a string MasterName...

Not:

return View("Something",id);

My recommendation here is that it can be more clear, and fix this problem, if you provide the parameter names

But Instead:

return View(viewName:"Something", model: id);

These types of problems can happen also in HTML Helper methods, where you often just expect to be able to pass in your model, but if you model is just a string, you might be matching some other overload method signature. (For example, the Action link html helpers.)

Because of this, I recommend just specifying the parameter name, and avoiding the unexpected. Heck, it might even make the expectation of your code just a little more clear.

Greg
  • 2,410
  • 21
  • 26