在代码(C#)中设置Facebook应用程序web.config设置
作者:互联网
我正在尝试通过代码在Facebook应用程序中设置web.config设置,以避免直接使用web.config文件.
我尝试了一个自定义ConfigurationSection类,然后使用WebConfigurationManager来访问web.config文件.问题是我无法获得Configuration对象的实例.这是我的代码:
public class FacebookConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("appId")]
public string AppID
{
get { return (string)base["appId"]; }
set { base["appId"] = value; }
}
[ConfigurationProperty("appSecret")]
public string AppSecret
{
get { return (string)base["appSecret"]; }
set { base["appSecret"] = value; }
}
[ConfigurationProperty("canvasPage")]
public string CanvasPage
{
get { return (string)base["canvasPage"]; }
set { base["canvasPage"] = value; }
}
[ConfigurationProperty("canvasUrl")]
public string CanvasUrl
{
get { return (string)base["canvasUrl"]; }
set { base["canvasUrl"] = value; }
}
[ConfigurationProperty("cancelUrlPath")]
public string CancelUrlPath
{
get { return (string)base["cancelUrlPath"]; }
set { base["cancelUrlPath"] = value; }
}
public FacebookConfigurationSection()
{
}
}
以及使用该页面的页面:
protected void Button1_Click(object sender, EventArgs e)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
FacebookConfigurationSection _config = new FacebookConfigurationSection();
_config = config.GetSection("facebookSettings") as FacebookConfigurationSection;
//FacebookConfigurationSection config = (FacebookConfigurationSection)System.Configuration.ConfigurationManager.GetSection("facebookSettings");
if (!string.IsNullOrEmpty(TextBox1.Text))
_config.AppID = TextBox1.Text.ToString();
if (!string.IsNullOrEmpty(TextBox2.Text))
_config.AppSecret = TextBox2.Text.ToString();
if (!string.IsNullOrEmpty(TextBox3.Text))
_config.CanvasPage = TextBox3.Text.ToString();
if (!string.IsNullOrEmpty(TextBox4.Text))
_config.CanvasUrl = TextBox4.Text.ToString();
_config.CancelUrlPath = "";
config.Save();
}
web.config看起来像这样(我正在尝试使用的部分):
<configSections>
<section type="Facebook.FacebookConfigurationSection, Facebook" name="facebookSettings" allowLocation="true" allowDefinition="Everywhere"/>
</configSections>
<facebookSettings
appId = "xxxxxxxxxxxxxxx"
appSecret = "xxxxxxxxxxxxxxxxxxxxxxxxx"
canvasPage = "xxxxxxxxxxxxxxxxxx"
canvasUrl ="xxxxxxxxxxxxxxxxxx"
cancelUrlPath = "" />
这样做给了我“对象引用未设置为对象的实例”.在_config上,它告诉我什么也没有返回.
有什么“特定于Facebook”的原因吗?
另一件事;我在代码中遇到了使用Facebook设置的这种新方法:
FacebookContext.SetApplication( IFacebookApplication )
我还没有找到一个使用此示例的好例子.
以前有没有人为此工作过?
解决方法:
只需使用
var sec = ConfigurationManager.GetSection("facebookSettings");
FacebookConfigurationSection config = (sec as Facebook.FacebookConfigurationSection);
config.AppID等
标签:facebook,web-config,settings,c 来源: https://codeday.me/bug/20191208/2093150.html