mvc5 attribute routing within area can't find view
Asked Answered
B

1

16

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?

Barranca answered 11/12, 2013 at 10:17 Comment(1)
May help some #22437073Kloman
G
25

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

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

You can find the documentation here.

Gittel answered 11/12, 2013 at 11:24 Comment(4)
Strangely this gives HTTP Error 404.0 - Not Found on the Index action.Barranca
Found what's causing it. When you use RouteArea you should remove RoutePrefix.Barranca
You can also include a prefix (different than the area name) like this [RouteArea("Admin", AreaPrefix = "myprefix")]Yumuk
Just [RouteArea] was enough for meJerricajerrie

© 2022 - 2024 — McMap. All rights reserved.