首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC区域-控制器不返回视图

MVC区域-控制器不返回视图
EN

Stack Overflow用户
提问于 2016-11-11 12:45:39
回答 1查看 1K关注 0票数 2

我在MVC项目中使用领域时遇到了一些问题。我能够访问位于该区域下的控制器,但是当它返回到视图(带有模型)时,我会得到一个错误:

没有找到视图'Index‘或其主目录,或者没有视图引擎支持搜索的locations.The,搜索了以下位置:~/Views/MyController/Index.aspx ~/Views/MyController/Index.ascx等等。

下面是MyAreaAreaRegistration:

代码语言:javascript
复制
public override void RegisterArea(AreaRegistrationContext context)
{
     context.MapRoute(
        "MyArea_default",
        "MyArea/{controller}/{action}/{id}",
        new { controller = "MyController", action = "Index", 
        id = UrlParameter.Optional });
}

Routeconfig:

代码语言:javascript
复制
routes.MapRoute(
                name: "Default", // Route name
                url: "{controller}/{action}/{id}", // URL with parameters
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "MyApp.Controllers" }
            );

Global.asax:

代码语言:javascript
复制
protected void Application_Start()
{
     AreaRegistration.RegisterAllAreas();
     ...
     RouteConfig.RegisterRoutes(RouteTable.Routes);
}

主计长:

代码语言:javascript
复制
return View(myViewModel);

我完全被这个困住了。任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2016-11-11 12:54:38

请更改您的路由配置代码。

代码语言:javascript
复制
routes.MapRoute(
                 name: "Default", // Route name
            url: "{controller}/{action}/{id}", // URL with parameters
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            ).DataTokens.Add("area", "MyArea"); ;

这将是你的工作。

谢谢。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40548333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档