ASP.NET MVC - Attribute Routing Does Not Find Area View

I have been looking for answers to all this everywhere, but I cannot find them. I basically have an MVC application setup, and I use the built-in AttributeRouting for my routes.

The folder structure is as follows:

  • Models
  • representation
  • Controllers
  • Areas
    • Users
      • MemberAreaRegistration.cs
      • Controllers
        • HomeController.cs
      • Views
        • home
          • Account.cshtml

And then I will connect my routes in global.asax as follows:

 public class Application : System.Web.HttpApplication { protected void Application_Start(){ AreaRegistration.RegisterAllAreas(); // other web optimization stuff RouteConfig.RegisterRoutes(RouteTable.Routes); } } 

So MemberAreaRegistration.cs is simple.

 namespace App.Web.Areas.Member { public class MemberAreaRegistration: AreaRegistration { public override string AreaName { get { return "Member"; } } } public override void RegisterArea( AreaRegistrationContext context){ } } 

And I'm trying to connect it using attributes ...

/areas/member/controllers/homecontroller.cs

 // ... [Route("member/account")] public ActionResult Account() { return View(); } // ... 

The problem is that it finds the route, but it cannot find the representation. I get the following error:

The "Account" view or its wizard is not found or if the viewing mechanism does not support the found locations. The following places were searched:

~ / Views / Home / Account.aspx

~ / Views / Home / Account.ascx

~ / Views / Shared / Account.aspx

~ / Views / Shared / Account.ascx

~ / Views / Home / Account.cshtml

~ / Views / Home / Account.vbhtml

~ / Views / Shared / Account.cshtml

~ / Views / Shared / Account.vbhtml

By all accounts, this should work fine - and if not, I expect ~/area to at least be on the path that the search is trying to do. Do I have to connect something extra to perform this function?

I am using ASP.NET MVC 5.0

If I rigidly set the absolute presentation path, it works. Obviously, this is not a good situation. I would prefer that he find a representation outside the convention. But if I type return View("~/areas/member/views/home/account.cshtml"); I will get the view back, so I know that it can access the file and that it is correct.

Here is my RouteConfig.cs per request

RouteConfig.cs

 public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { // mvc attribute routing allows us to supersede normal routing mechanisms and // declare our routes a bit more verbosely routes.MapMvcAttributeRoutes(); routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new[] { "App.Web.Controllers" } ); } } 
+6
source share
1 answer

This is because, once you define your route as an attribute of an action, ASP.NET MVC does not know what area it is in, so it does not know where to look for Views.

There is an Account action in the controller, try explicitly specifying the RouteArea attribute.

I am writing this from the head, but it should look like this:

 [RouteArea("Member")] [RoutePrefix("member")] public class HomeController: Controller { [Route("account")] public ActionResult Account() { return View(); } } 

or alternatively:

 [RouteArea("Member")] public class HomeController: Controller { [Route("member/account")] public ActionResult Account() { return View(); } } 
+17
source

Source: https://habr.com/ru/post/984825/


All Articles