我试图在代码中设置facebook应用程序中的web.config设置,以避免直接使用web.config文件。 我试过了一个自定义的ConfigurationSection类,然后使用WebConfigurationManager来访问web.config文件。问题是我无法获得Configuration对象的实例。这是我的代码:在代码中设置facebook应用程序web.config设置(C#)
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设置这种新的工作方法:
FacebookContext.SetApplication(IFacebookApplication)
我一直没能找到一个使用一个很好的例子。 有没有人曾与此工作?
试过了,同样的错误... – Soeren 2011-03-02 11:05:56