我試圖在代碼中設置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