C# 在ASP.NET核心中全局重用变量

C# 在ASP.NET核心中全局重用变量,c#,asp.net-core,design-patterns,C#,Asp.net Core,Design Patterns,我必须强迫这些变量在我想使用的每个变量上重复使用,这让我很难。我需要创建一个类来定义这些变量,并在整个程序中使用它们。我该怎么做 string RootFolderName = "Uplaod"; string ProductPictureFolder = "ProductPictureFolder"; string ProductMainPictureFolder = "ProductMainPicture"; string WebRootPath = _hostingEnvironment.

我必须强迫这些变量在我想使用的每个变量上重复使用,这让我很难。我需要创建一个类来定义这些变量,并在整个程序中使用它们。我该怎么做

string RootFolderName = "Uplaod";
string ProductPictureFolder = "ProductPictureFolder";
string ProductMainPictureFolder = "ProductMainPicture";
string WebRootPath = _hostingEnvironment.WebRootPath;
string RootPath = Path.Combine(WebRootPath, RootFolderName);
string ProductPicturePath = Path.Combine(WebRootPath, RootFolderName, ProductPictureFolder);
string ProductMainPicturePath = Path.Combine(WebRootPath, RootFolderName, ProductPictureFolder, ProductMainPictureFolder);
string newPath = Path.Combine(WebRootPath, ProductMainPicturePath);

您可以使用singleton类,如下所示:

界面:

public interface IApplicationData
{
    string RootFolderName { get; }

    string ProductPictureFolder { get; }

    string ProductMainPictureFolder { get; }

    string WebRootPath { get; }

    string RootPath { get; }

    string GetProductPicturePath();

    string GetProductMainPicturePath();

    string GetNewPath();
}
public class ApplicationData : IApplicationData
{
    readonly IHostingEnvironment _hostingEnvironment;

    public ApplicationData(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }

    public string RootFolderName => "Upload";

    public string ProductPictureFolder => "ProductPictureFolder";

    public string ProductMainPictureFolder => "ProductMainPicture";

    public string WebRootPath => _hostingEnvironment.WebRootPath;

    public string RootPath => Path.Combine(WebRootPath, RootFolderName);

    public string GetProductPicturePath()
    {
        return Path.Combine(WebRootPath, RootFolderName, ProductPictureFolder);
    }

    public string GetProductMainPicturePath()
    {
        string path = Path.Combine(WebRootPath, RootFolderName, ProductPictureFolder, ProductMainPictureFolder);
        return path;
    }

    public string GetNewPath()
    {
        string productMainPicturePath = GetProductMainPicturePath();
        return Path.Combine(WebRootPath, productMainPicturePath);
    }
}
services.AddSingleton<IApplicationData, ApplicationData>();
public class ValuesController : ControllerBase
{
    readonly IApplicationData _applicationData;

    public ValuesController(IApplicationData applicationData)
    {
        _applicationData = applicationData;
    }

    [HttpGet]
    public IActionResult Get()
    {
        string data = _applicationData.ProductMainPictureFolder;
        string data2 = _applicationData.GetProductPicturePath();
        string data3 = _applicationData.GetNewPath();

        return Ok();
    }
}
具体实施:

public interface IApplicationData
{
    string RootFolderName { get; }

    string ProductPictureFolder { get; }

    string ProductMainPictureFolder { get; }

    string WebRootPath { get; }

    string RootPath { get; }

    string GetProductPicturePath();

    string GetProductMainPicturePath();

    string GetNewPath();
}
public class ApplicationData : IApplicationData
{
    readonly IHostingEnvironment _hostingEnvironment;

    public ApplicationData(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }

    public string RootFolderName => "Upload";

    public string ProductPictureFolder => "ProductPictureFolder";

    public string ProductMainPictureFolder => "ProductMainPicture";

    public string WebRootPath => _hostingEnvironment.WebRootPath;

    public string RootPath => Path.Combine(WebRootPath, RootFolderName);

    public string GetProductPicturePath()
    {
        return Path.Combine(WebRootPath, RootFolderName, ProductPictureFolder);
    }

    public string GetProductMainPicturePath()
    {
        string path = Path.Combine(WebRootPath, RootFolderName, ProductPictureFolder, ProductMainPictureFolder);
        return path;
    }

    public string GetNewPath()
    {
        string productMainPicturePath = GetProductMainPicturePath();
        return Path.Combine(WebRootPath, productMainPicturePath);
    }
}
services.AddSingleton<IApplicationData, ApplicationData>();
public class ValuesController : ControllerBase
{
    readonly IApplicationData _applicationData;

    public ValuesController(IApplicationData applicationData)
    {
        _applicationData = applicationData;
    }

    [HttpGet]
    public IActionResult Get()
    {
        string data = _applicationData.ProductMainPictureFolder;
        string data2 = _applicationData.GetProductPicturePath();
        string data3 = _applicationData.GetNewPath();

        return Ok();
    }
}
在DI容器中注册:

public interface IApplicationData
{
    string RootFolderName { get; }

    string ProductPictureFolder { get; }

    string ProductMainPictureFolder { get; }

    string WebRootPath { get; }

    string RootPath { get; }

    string GetProductPicturePath();

    string GetProductMainPicturePath();

    string GetNewPath();
}
public class ApplicationData : IApplicationData
{
    readonly IHostingEnvironment _hostingEnvironment;

    public ApplicationData(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }

    public string RootFolderName => "Upload";

    public string ProductPictureFolder => "ProductPictureFolder";

    public string ProductMainPictureFolder => "ProductMainPicture";

    public string WebRootPath => _hostingEnvironment.WebRootPath;

    public string RootPath => Path.Combine(WebRootPath, RootFolderName);

    public string GetProductPicturePath()
    {
        return Path.Combine(WebRootPath, RootFolderName, ProductPictureFolder);
    }

    public string GetProductMainPicturePath()
    {
        string path = Path.Combine(WebRootPath, RootFolderName, ProductPictureFolder, ProductMainPictureFolder);
        return path;
    }

    public string GetNewPath()
    {
        string productMainPicturePath = GetProductMainPicturePath();
        return Path.Combine(WebRootPath, productMainPicturePath);
    }
}
services.AddSingleton<IApplicationData, ApplicationData>();
public class ValuesController : ControllerBase
{
    readonly IApplicationData _applicationData;

    public ValuesController(IApplicationData applicationData)
    {
        _applicationData = applicationData;
    }

    [HttpGet]
    public IActionResult Get()
    {
        string data = _applicationData.ProductMainPictureFolder;
        string data2 = _applicationData.GetProductPicturePath();
        string data3 = _applicationData.GetNewPath();

        return Ok();
    }
}

你说的每次重建是什么意思?您不能使用静态类或单例类吗?@MoienTajik i使用静态类,但它在主机环境中显示错误。WebRootPath不能使用静态类、方法或property@MoienTajikMean
重用
我的错误正如@MoienTajik所说,使用静态类或单例类。singleton类将帮助您访问局部变量\u hostingEnvironment。如果使用静态类,则可以将_hostingEnvironment.WebRootPath作为参数传递给method@YvesIsrael我不想
Class c=newclass()
Class