【问题标题】:MVC application Viewengine not looking in Areas/{name}/Views instead looking in default Views folderMVC 应用程序 Viewengine 不在 Areas/{name}/Views 中查找,而是在默认 Views 文件夹中查找
【发布时间】:2017-12-07 01:18:17
【问题描述】:

我正在使用 2017 VC for mac,xamarian 版本的 vs 没有自动区域映射器。我手动添加了 Areas 文件夹并正确配置了路由,当我将 actionLink 添加到我的 csHTML 主页面时,它会抛出 500 错误并声明。

System.InvalidOperationException
The view 'Billing' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/BillingMain/Billing.cshtml
~/Views/BillingMain/Billing.vbhtml
~/Views/Shared/Billing.cshtml
~/Views/Shared/Billing.vbhtml
~/Views/BillingMain/Billing.aspx
~/Views/BillingMain/Billing.ascx
~/Views/Shared/Billing.aspx
~/Views/Shared/Billing.ascx

它应该在~/Areas/Billing/Views/BillingMainController.cshtml中搜索

我已经尽一切努力让它工作,但只是一直卡住。

这是我的目录层次结构

Project-
    AppStart-
    Areas-
        Billing-
        Invoice-
    Controllers-
    Scripts-
    Views-
    Global.asax

我现在只测试计费区域,下面是我的其余代码。

路由配置

using System.Web.Mvc;
using System.Web.Routing;

namespace WORK_GODDAMN
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            //AreaRegistration.RegisterAllAreas();
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                //,namespaces: new string[] { "WORK_GODDAMN.Areas.Controllers" }
            );

        }
    }

}

BillingAreaRegistration.cs

using System;
using System.Web.Mvc;

namespace WORK_GODDAMN.Areas.Billing
{

    public class BillingAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Billing";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Billing_Default",
                "Billing/{controller}/{action}/{id}",
                new {controller = "BillingMain", action = "Index", id = UrlParameter.Optional }
                , namespaces: new string[] { "WORK_GODDAMN.Areas.Billing.Controllers" }
            );

        }   
    }



}

全球.asax

using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Http;

namespace WORK_GODDAMN
{
    public class Global : HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }
    }
}

main _Layout.cshtml 这是 Action Link 重定向的地方,我认为我可能在这里做错了,但目前我不知道是什么导致了问题。

<!DOCTYPE html>

<html>
    @using System.Web.Optimization
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title</title>
    @Scripts.Render("~/Scripts/modernizr.js")
</head>
<body>
    <div>
        <div>
            <h2>{Demo}- Pluggable Modules</h2>
        </div>
        <div id="nav">
            @Html.ActionLink("Home","Index","Home", new {Area=""}, null) 
            @Html.ActionLink("Invoice","Index","InvoiceMain", new {Area=""}, null) 
            @Html.ActionLink("Billing","Index","BillingMain", new {Area=""}, null)
        </div>
        <hr />
        <div>
                @RenderBody()
        </div>
    </div>
    @Scripts.Render("~/Scripts/jquery.js")
    @RenderSection("Scripts", required: false)
</body>
</html>

【问题讨论】:

  • 您要访问的网址是什么?
  • localhost:8080/BillingMain 应该路由到Views/Billing/BillingMainController
  • 你不应该在 url 中有区域名称吗?目前我猜它没有击中区域控制器。试试Billing/BillingMain
  • 导致404 not found 错误?我之前尝试过,因为这在逻辑上是它应该映射到的地方,但没有用。我不得不手动创建 Areas 文件夹,因为 VS mac 2017 没有 add->Areas 选项

标签: c# html asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

想通了,错误发生在我的控制器中。我没有返回return View("Index"),而是给出了绝对路径return View("~/Areas/Billing/Views/Index.cshtml"),这是一种解决方法,这是我发现的唯一可行的方法。

另外,将来如果有人想使用 html 动作链接和模型标签,您将需要在 Areas/{area}/Views 和 Areas 文件夹中手动添加 Web.Config 文件,以便您可以在 cshtml 中使用 MVC 框架查看文件。 这适用于必须手动创建区域的任何人,如果您使用 VS 工具添加->如果您有 VS 执行区域 VS 将自动生成所需的文件,因为我使用Mac Xamarin VS 我没有那个方便的选项。

这是我必须改变的。

在 {Project/Areas/Billing/Views/Index.cshtml} 更改

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

namespace WORK_GODDAMN.Controllers
{

    public class BillingMainController : Controller
    {
        // GET: Billing/BillingMain

        public ActionResult Index()
        {
            return View("Index");
        }
    }
}

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

namespace WORK_GODDAMN.Controllers
{

    public class BillingMainController : Controller
    {
        // GET: Billing/BillingMain

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

【讨论】:

    【解决方案2】:

    假设您在 Billing 区域内有 BillingMain 控制器,您应该在使用 ActionLink 时明确指定区域名称。

    @Html.ActionLink("Billing","Index","BillingMain", new {Area="Billing"}, null)
    

    这将生成带有指向yourSiteBaseAddress/Billing/BillingMain/Index的href值的链接

    假设您没有任何客户路线定义来覆盖默认约定,只要您在 ~/Areas/Billing/Views/BillingMain/~/Areas/Billing/Views/Shared/ 中拥有视图文件 Index.cshtml,它应该可以工作(假设您不尝试返回不同的从索引操作查看文件)

    【讨论】:

    • 当我在 ActionLink 中明确指定区域时,我仍然收到 404 not found 错误。我在~/Areas/Billing/Views/BillingMain 中有一个Index.cshtml
    • 您在尝试访问 yourSiteBaseAddress/Billing/BillingMain/Index 时收到 404 错误?
    • 由于某种原因,我认为区域没有正确注册,因此视图引擎正在默认文件夹中搜索,而我是 VS 的新手,我不确定调试过程的实际情况。
    • 是的,当我访问 localhost:8080/Billing/BillingMain/Index 时,我仍然收到 404
    • 我将添加我的文件夹的图片,所有内容都已展开
    【解决方案3】:

    在 Html.ActionLink 中指定区域的名称。

    @Html.ActionLink("Billing", "Index", "BillingMain", new { Area = "Billing" }, null)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-27
      • 1970-01-01
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 2016-03-25
      相关资源
      最近更新 更多