开发者

Why do ASP.NET MVC3 Areas and Razor Views produce this error?

开发者 https://www.devze.com 2023-01-13 01:58 出处:网络
The view 开发者_开发问答at \'~/Areas/SomeArea/Views/List/Index.cshtml\' must derive from ViewPage, ViewPage, ViewUserControl, or ViewUserControl.

The view 开发者_开发问答at '~/Areas/SomeArea/Views/List/Index.cshtml' must derive from ViewPage, ViewPage, ViewUserControl, or ViewUserControl.

The project structure is pretty much default. There is one area called SomeArea. It has a single controller called List. It does nothing except:

public ActionResult Index()
{
   return View("~/Areas/SomeArea/Views/List/Index.cshtml");
}

The view looks like:

@inherits System.Web.Mvc.WebViewPage<dynamic>

@{
    View.Title = "Index";
    LayoutPage = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

I have tried emptying the entire file part by part and nothing seems to help. If I create a controller and view outside the area it works just fine. Is it possible the default razor view engine doesn't support areas at this time?

Edit: The areas are registered.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Random", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
}

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "SomeArea_default",
        "SomeArea/{controller}/{action}/{id}",
        new { controller = "List", action = "Index", id = UrlParameter.Optional }
    );
}


An answer from the ASP.NET Forums:

http://forums.asp.net/t/1593209.aspx

This fixed the problem. Thanks to the replier!


Getting the error "The view at '~/Views/Page/home.aspx' must derive from ViewPage, ViewPage<TViewData>, ViewUserControl, or ViewUserControl<TViewData>" Similar question, might help you. Also, did you register your areas in the global.asax?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号