mvc5 attribute routing within area can't find view

7.1k views Asked by At

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?

1

There are 1 answers

4
James On BEST ANSWER

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

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

You can find the documentation here.