如何使用| DataDirectory |用asp.net核心替换appsettings.json中的字符串? [英] How to use |DataDirectory| substitution string in appsettings.json with asp.net core?

查看:54
本文介绍了如何使用| DataDirectory |用asp.net核心替换appsettings.json中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是asp.net核心的新手,我想做的任务应该非常简单.使用Visual Studio,我试图将.mdf文件作为本地数据库链接到我的项目.由于我想使其适用于多台计算机,因此我需要从appsettings.json查找数据目录文件夹路径.因此,经过一些研究,最好的方法是使用 | DataDirectory | 替换字符串.

I'm new to asp.net core and the task I want to do should be very simple. Using Visual Studio, I'm trying to link a .mdf file to my project as a local database. As I want to make it work for several computers, I need to find the data directory folder path from appsettings.json. Therefore, after some researches, the best way to do that is using the |DataDirectory| substitution string.

问题是我的网站无法以这种方式访问​​我的mdf文件,并生成ArgumentException:键'attachdbfilename'的值无效" .尽管我找到了有关此问题的一些主题,但这些主题都没有回答我的问题.

The problem is that my website can't reach my mdf file this way and it generates an ArgumentException : "Invalid value for key 'attachdbfilename'". Although I found some topics about this issue, none of these answered to my question.

我已经尝试使用完整路径来查找我的文件,并且该文件可以工作,但是正如我所说,我需要从多台计算机中找到它.

I've already tried to use the full path to find my file and it worked but as I said, I need to find it from several computers.

这是Startup.cs和appsettings.json

Here are Startup.cs and appsettings.json

Startup.cs :

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            string path = Path.Combine(Directory.GetCurrentDirectory(), "App_Data");
            AppDomain.CurrentDomain.SetData("DataDirectory", path);

            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddDefaultIdentity<IdentityUser>()
                .AddEntityFrameworkStores<ApplicationDbContext>();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }
        [...]
   }

我的连接字符串,位于 appsettings.json 中:

My connection string, in appsettings.json :

"ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;AttachDbFilename=|DataDirectory|\\aspnet-MatrixCalculatorApp-db.mdf;Trusted_Connection=True;MultipleActiveResultSets=true"
  },

如果需要,我还可以提供堆栈跟踪.

If needed, I can also provide the stack trace.

预先感谢您的帮助.

推荐答案

好吧,如果有人仍然遇到与我相同的问题,我只是找到了解决方法:

Well, if someone still has the same issue as I had, I just found a solution :

您只需将字符串的出现替换为数据文件夹的路径即可.

You can simply replace the occurence of a string, with the path of your data folder.

Startup.cs :

string path = Path.Combine(Directory.GetCurrentDirectory(), "App_Data");

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection").Replace("[DataDirectory]", path)));
            services.AddDefaultIdentity<IdentityUser>()

appsettings.json

"DefaultConnection": "Server=(localdb)\\mssqllocaldb;AttachDbFilename=[DataDirectory]\\aspnet-MatrixCalculatorApp-db.mdf;Trusted_Connection=True;MultipleActiveResultSets=true"

我替换了| DataDirectory |使用 [DataDirectory] ​​可以避免程序与替换字符串混淆.但是,如果有人比我有更好的解释,那将是很好的选择.

I replaced |DataDirectory| with [DataDirectory] to avoid program confusion with the substitution string. But if someone has a better explanation than me, it would be nice to do it.

这篇关于如何使用| DataDirectory |用asp.net核心替换appsettings.json中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆