ASP.NET Core Razor Pages,有没有办法配置链接目录文件夹路径?

ASP.NET Core Razor Pages, is there a way to configure a linked directory folder path?

我想在 appsettings.json 文件中配置我想在 razor 页面上的网络共享目录中的文件链接,但我无法找到使用内部配置密钥的方法cshtml 锚标记。有没有办法使用剃刀语法来做到这一点?

在此先感谢您的帮助。

我假设您已经将目录路径存储在 appsettings.json 文件中,并且您想从 appsettings.json 文件中获取路径并使用锚标记中的值,对吗?如果是这样,您可以尝试使用以下方法来使用 Razor Pages 中的配置。

在appsettings.json文件中添加文件节点:

    { 
      "Logging": {
        "LogLevel": {
          "Default": "Information",
          "Microsoft": "Warning",
          "Microsoft.Hosting.Lifetime": "Information"
        }
      },
      "File": {
        "FilePath": "https://docs.microsoft.com/"
      },
      "AllowedHosts": "*"
    }
  1. 在 Razor 页面中使用 @inject 指令(index.cshtml)。

         @using Microsoft.Extensions.Configuration
         @inject IConfiguration Configuration 
    
         <a href="@Configuration.GetSection("File")["FilePath"]">Microsoft</a>
    
  2. 使用ViewData或ViewBag存储路径,并显示在锚标签中

    .cshtml.cs 文件中的代码:

     public class IndexModel : PageModel
     {
         private readonly IConfiguration _configuration;
         public IndexModel(IConfiguration configuration)
         {
             _configuration = configuration;
         }
         public void OnGet()
         {           
             ViewData["config"] = _configuration["File:FilePath"]; //get the configuration key value.
         } 
     }
    

    视图页面中的代码(.cshtml):

         <a href="@ViewData["config"]">Microsoft</a>
    

结果是这样的: