Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvc5 attribute routing within area can't find view

When I'm inside Admin area and map my routes using attribute routing it cannot find view because it doesn't look inside actual area view folders but instead only global view folders.

Only if I pass full path to view it then can display it, otherwise it throws me error.

Error

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

Code

[RoutePrefix("admin")]
public class HomeController : Controller
{

    [Route]
    public ActionResult Index()
    {
        return View("Authorize"); // Error
        return View("~/Areas/Admin/Views/Home/Authorize.cshtml"); // Working
    }
}

Note that if I disable attribute routing and switch back to good old routes it will work. Any way of fixing this or it's working as intended and I should apply full path in all my areas?

like image 342
Stan Avatar asked Dec 11 '13 10:12

Stan


People also ask

Can you enable attribute routing in MVC 5?

Enabling attribute routing in your ASP.NET MVC5 application is simple, just add a call to routes. MapMvcAttributeRoutes() method with in RegisterRoutes() method of RouteConfig. cs file.

Can we combine both attribute routing and convention based routing in a single application?

Attribute routing gives you more control over the URIs in your web API. For example, you can easily create URIs that describe hierarchies of resources. The earlier style of routing, called convention-based routing, is still fully supported. In fact, you can combine both techniques in the same project.

Where is RouteConfig in MVC?

Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder.


1 Answers

You need to add the [RouteArea("")] attribute to your controller:

[RouteArea("Admin")]
public class HomeController : Controller

You can find the documentation here.

like image 73
James Avatar answered Oct 16 '22 14:10

James