简体   繁体   English

在Visual Studio解决方案中的跨项目的web.config中共享变量

[英]Share variable in web.config Across Projects in a Solution in Visual Studio

I have one solution that contains 3 projects : ASP.net MVC 4.0, Share Library and Web Service project. 我有一个包含3个项目的解决方案:ASP.net MVC 4.0,Share Library和Web Service项目。 I want to share some of key value in appSettings of Web.Config in ASP.net mvc Project to Web Service Project. 我想将ASP.net mvc项目中Web.Config的appSettings中的一些关键值共享给Web服务项目。 I found this answer but it's now working for me. 我找到了这个答案,但现在对我有用。

Could anyone give me some idea please. 任何人都可以给我一些想法。

Thanks you so much. 非常感谢。

You could store that information into a dll that you would share accross all your projects : 您可以将该信息存储到将在所有项目中共享的dll中:

Create a new Class Library project : 创建一个新的类库项目:

Name your class MyConfiguration.cs for example 例如,将您的类命名为MyConfiguration.cs

namespace CentralizedInformation
{
    public static class MyConfiguration
    {
        public static string Value1 = "Test string value 1";
        public static int Value2 = 2;
    }
}

After you can add the reference in all your projects (Project -> add reference -> Browse -> Browse to CentralizedInformation.dll) 在您可以在所有项目中添加引用之后(项目->添加引用->浏览->浏览到CentralizedInformation.dll)

and add itin your projects (here an mvc project example) 并将其添加到您的项目中(这里是一个mvc项目示例)

using CentralizedInformation;

//for asp net mvc project 
protected void Application_Start()
{
   string mySharedAccrossAllProjectsString = CentralizedInformation.MyConfiguration.Value1;
   //put it wherever you need it session or anything else
}

Juste reference the same in your other projects and you are done. Juste在其他项目中引用相同的内容,您就完成了。 Also rebuild your projects when you want to update the data ... 当您要更新数据时,还可以重建项目...

That dll could have a config file and you could programmatically expose all it's data. 该dll可能有一个配置文件,您可以以编程方式公开其所有数据。

I admit it's a quite funny and complicated way to do such a simple thing. 我承认这是做这种简单事情的一种非常有趣且复杂的方法。 But that's a way ^^ 但这是一种方式^^

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Visual Studio 2015 中的一个解决方案中跨多个 ASP.NET CORE 项目共享 appsettings.json 等配置文件? - How can I share config file like appsettings.json across multiple ASP.NET CORE projects in one solution in Visual Studio 2015? 核心类库和.net Core 2 MVC之前版本的解决方案-web.config连接字符串不起作用 - Solution with pre Core Class Library & .net Core 2 MVC projects - web.config connectionstring not working ASP.NET Core 2.2 如何在项目之间共享 web.config/launchSettings.json 文件 - ASP.NET Core 2.2 How to share web.config/launchSettings.json files among projects 如何在 Visual Studio Web 2013 中添加 app.config 或 web.config 文件? - How can I add an app.config or web.config file in Visual Studio Web 2013? 如何停止Visual Studio Web将web.config写入/ obj目录 - How to stop Visual Studio Web writing web.config to /obj directory mvc3网站web.config中的visual studio无法识别ssl和rewrite标记 - ssl and rewrite tag does not recognized by visual studio in mvc3 web site web.config Visual Studio 2012 ASP.NET MVC连接字符串Web.Config - Visual Studio 2012 ASP.NET MVC Connection Strings Web.Config ASP.NET MVC 2 使用 Visual Studio 2010。通过 web.config 更改角色管理器 - ASP.NET MVC 2 using Visual Studio 2010. Changing the Roles Manager through web.config 为什么 AutogenerateBindingRedirects 不适用于 Visual Studio 2017 中的 Web.config - Why doesn't AutogenerateBindingRedirects work for a Web.config in Visual Studio 2017 如何在同一解决方案中跨项目共享css和js文件? - How to share css and js files across projects in the same solution?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM