【问题标题】:New Area is Razor Pages is not displaying the root _Layout.cshtml file新区域是 Razor 页面不显示根 _Layout.cshtml 文件
【发布时间】:2022-03-04 13:10:55
【问题描述】:

我正在为 asp.net core 开发一个新的 Razor Page 应用程序。

问题:我添加了一个新的 Area 文件夹 (TeamDrawing),但是当我将 _ViewStart.cshtml 放在 Area/TeamDrawing/Pages 中时,对 Root/Pages/Shared/_Layout.cshtml 页面的引用不起作用文件夹或 Area/TeamDrawing 文件夹。

我在 Area/Identity 文件夹中使用 asp.net 身份(内置在我使用的模板中),它引用 Root/Pages/Shared/_Layout.cshtml 没有问题。

我做错了什么?

我想在我的所有区域文件夹中使用我的 Root/Pages/Shared/_Layout.cshtml。

这是我的区域文件夹:

这是根页面文件夹:

更新:

我的问题似乎是别的。我没有意识到我有一个 F12 控制台错误。 Root/Pages/Shared/_Layout.cshtml 调用一些代码来加载我的菜单项,并且路径试图转到 Area/TeamDrawing 中的控制器,而不是根上的控制器。我需要弄清楚如何指向根/控制器。

所以在根目录中指向 _Layout 似乎没问题。

更新 2:

如果我将直接 URL 放在 get 调用中,它将起作用。

有没有办法不能将直接 URL 放在 URL 中

作品:

    $.get(BuildSafeURL("https://localhost:44355/ToolbarMenu/LoadToolbarMenuItems", null))
        .done(function (data) {

        });

因为它试图进入 Area/TeamDrawing 中的控制器而中断:

  $.get(BuildSafeURL("ToolbarMenu/LoadToolbarMenuItems", null))
        .done(function (data) {
        
        });

我已经在我的 Root/_Layout 文件中尝试过这个:

    <script type="text/javascript">
        // Global variable for relative pathing for ajax calls
        var rootPath = '@Url.Content("~")';
    </script>

但这不适用于rootPath

【问题讨论】:

    标签: c# asp.net-core razor-pages


    【解决方案1】:

    如果你不想去Area/TeamDrawing中的控制器,尝试使用:

    $.get(BuildSafeURL("/ToolbarMenu/LoadToolbarMenuItems", null))
            .done(function (data) {
            
            });
    

    【讨论】:

    【解决方案2】:

    我现在可以工作了:

    完整代码:

    根_Layout.cshtml:

    @using Microsoft.Extensions.Configuration
    @inject IConfiguration Configuration
        <script>      
             const rootPath = @Json.Serialize(@Configuration.GetSection("ApiUrls").GetSection("commonUrl").Value + "/")
        </script>
    

    appsetting.js

    {
      "ApiUrls": {
        "commonUrl": "https://localhost:44355"
      }
    }
    

    app.js -> BuildSafeUrl()(调用get到控制器获取菜单项时使用)

    function BuildSafeURL(serverPath, parameterCollection) {
    
        // Validate the serverPath is well formed
        serverPath = ValidateServerPath(serverPath);
    
        // Append the rootPath to handle virtual directory pathing
        var path = rootPath + serverPath;
    
        // Encode and append any search parameters
        if (parameterCollection && parameterCollection.length > 0) {
            path += "?";
    
            var searchParameters = new URLSearchParams();
    
            for (var i = 0; i < parameterCollection.length; i++) {
    
                var key = parameterCollection[i].Key;
                var value = parameterCollection[i].Value;
    
                searchParameters.append(key, encodeURIComponent(value));
            }
    
            path += searchParameters.toString();
        }
        return path;
    }
    
    function ValidateServerPath(serverPath) {
        if (!serverPath) {
            return '';
        }
        else if (serverPath === '/') {
            return '';
        }
        else if (serverPath.charAt(0) === '/') {
            return serverPath.substring(1);
        }
        else {
            return serverPath;
        }
    }
    

    toolbarMenu.js -> 我在 Area/TeamDrawing 文件夹中并在根目录(在 Area 文件夹之外)调用控制器的位置

    function loadToolbarMenuList() {
        $.get(BuildSafeURL("ToolbarMenu/LoadToolbarMenuItems", null))
            .done(function (data) {
              
            })
            .fail(error =>
            {
                console.log(error);
            })
    }
    

    现在我的菜单项在 Area/TeamDrawing/Pages/GenerateChristmasList 上正确显示

    【讨论】:

      猜你喜欢
      • 2019-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      • 2015-12-20
      • 2022-01-21
      • 2021-07-27
      • 1970-01-01
      相关资源
      最近更新 更多