在Area中找不到视图

时间:2014-08-01 10:32:22

标签: asp.net-mvc asp.net-mvc-5 asp.net-mvc-routing

我创建了Area In ASP.NET MVC。如下所示。,

enter image description here

CategoryController我写Index Action就像这样(在AdminArea内部的CategoryController)

 public partial class CategoryController : Controller
 {
      public virtual ActionResult Index()
      {
          return View();
      }
 }

AdminAreaRegistration

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

并创建这样的链接。

@Html.ActionLink("sss",MVC.Admin.Category.ActionNames.Index,MVC.Admin.Category.Name)

当我运行网站时,点击链接时出现此错误。

  

未找到视图“索引”或其主控或没有查看引擎   支持搜索的位置。以下地点是   搜寻:

~/Views/Category/Index.aspx
~/Views/Category/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Category/Index.cshtml
~/Views/Category/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

1 个答案:

答案 0 :(得分:1)

我怀疑你从Controller and action调用link的方式。还有为什么我们需要virtual keyword in ActionResult。所以,请像下面这样......

Its working for me

在您的管理区域注册

using System.Web.Mvc;

namespace WebApplication2.Areas.Admin
{
    public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }

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

在您的类别控制器中:删除 virtual 关键字。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace TestScoff.Areas.Admin.Controllers
{
    public class CatgoryController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}

您的链接应如下所示:

@Html.ActionLink("Click Here to Go Catgory", "Index", "Catgory")

注意: 在浏览器中,您的链接将会是这样的 http://localhost:54230/Admin/Catgory

Click here for simple reference