在Web.Config文件中存储链接的最佳标记是什么?

Ahm*_*rid 1 asp.net sharepoint web-config

我可能把网站放在不同的服务器上,它有链接链接,特别是因为它在sharepoint上,所以有地址和端口,所以我认为最好将链接保存在Web.Config文件中,以便不需要更改它很多.那么最佳位置或标记放在哪里,例如连接字符串位于:configuration-> connectionstrings.

Fre*_*örk 5

我有时习惯appSettings存储链接模板

<configuration>
  <appSettings>
    <add key="link.template1" value="http://example.com:1234/" />
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

请注意,url需要进行xml编码,因此如果它包含查询字符串参数,则需要将&字符编码为&amp;(并可能编码其他字符):

<configuration>
  <appSettings>
    <add key="link.template1" value="http://example.com/p1=value&amp;p2=othervalue" />
  </appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)

......当然,我应该暗示如何使用这些值.感谢@ Tchami在评论中提供了答案; 在这里添加了答案完整性:

string urlTemplate = ConfigurationManager.AppSettings["link.template1"];
Run Code Online (Sandbox Code Playgroud)

如果您还没有它,则需要添加System.Configuration对项目的引用,以及using System.Configuration代码文件开头的语句.