mvc 默认访问 Area 下控制器方法

在MVC项目中经常会使用到Area来分开不同的模块让项目结构更加的清晰。如果想网站打开默打开Area下的控制器时会出现以下的错误

“/”应用程序中的服务器错误。

未找到视图“Index”或其母版视图,或没有视图引擎支持搜索的位置。搜索了以下位置: 
~/Views/Default/Index.aspx
~/Views/Default/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Default/Index.cshtml
~/Views/Default/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

解决方法:

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.MapRoute(
      name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional},
      namespaces:new string[]{"MVCUI.Areas.自己Area的名称.Controllers"}
      ).DataTokens.Add("Area","自己Area的名称");
}

超链接使用法:

return RedirectToAction("action", "controller", new { area = "area-name" });
<%= Html.ActionLink("超链接内容", "action", "controller", new { area = "area-name" }, null) %>

猜你喜欢

转载自www.cnblogs.com/superfeeling/p/9221265.html