在代码中设置facebook应用程序web.config设置(C#)

时间:2011-03-02 10:41:50

标签: c# facebook web-config settings

我正在尝试从代码中设置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 )

我无法找到一个使用它的好例子。 有没有人曾经使用过这个?

5 个答案:

答案 0 :(得分:2)

只需使用

var sec = ConfigurationManager.GetSection("facebookSettings"); 

FacebookConfigurationSection config = (sec as Facebook.FacebookConfigurationSection); 

config.AppID等

答案 1 :(得分:1)

尝试

section type="Facebook.FacebookConfigurationSection"

或者,如果你没有命名空间

section type="FacebookConfigurationSection"

我认为你注释掉的那行也不起作用?

FacebookConfigurationSection config = (FacebookConfigurationSection)System.Configuration.ConfigurationManager.GetSection("facebookSettings");

答案 2 :(得分:0)

问题可能在于您的<facebookSettings>部分,请按照以下格式进行尝试:

<facebookSettings>
    <add key="appId " value="xxxxxxxxxxxxxxxx" />
    <add key="appSecret " value="xxxxxxxxxxxxxxxxxxxxxxxxxxx" />
            ...
            ...
<facebookSettings>

答案 3 :(得分:0)

我不完全确定为什么你的代码不起作用,但尝试下面的内容,我已经尝试了一个带有web.config文件的示例,它确实有用

System.Configuration.ConfigurationSection sec = System.Configuration.ConfigurationManager.GetSection("facebookSettings");
Facebook.FacebookConfigurationSection _config = (sec as Facebook.FacebookConfigurationSection); 

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();

答案 4 :(得分:0)

好吧,我找不到任何关于使用

的好例子
FacebookContext.SetApplication( IFacebookApplication )

我不确定甚至可以用“ConfigurationSection”的方式来做...

所以,我作弊..

我将web.config文件加载到XmlDocument对象中并以这种方式对其进行操作......

XmlDocument XmlDoc = new XmlDocument();
    XmlDoc.Load(Server.MapPath("web.config"));

    XmlAttribute appId = XmlDoc.SelectSingleNode("//configuration//facebookSettings//@appId") as XmlAttribute;
    if (appId != null) appId.Value = TextBox1.Text.ToString();

    XmlAttribute appSecret = XmlDoc.SelectSingleNode("//configuration//facebookSettings//@appSecret") as XmlAttribute;
    if (appSecret != null) appSecret.Value = TextBox2.Text.ToString();

    XmlAttribute canvasPage = XmlDoc.SelectSingleNode("//configuration//facebookSettings//@canvasPage") as XmlAttribute;
    if (canvasPage != null) canvasPage.Value = TextBox3.Text.ToString();

    XmlAttribute canvasUrl = XmlDoc.SelectSingleNode("//configuration//facebookSettings//@canvasUrl") as XmlAttribute;
    if (canvasUrl != null) canvasUrl.Value = TextBox4.Text.ToString();

    XmlDoc.Save(Server.MapPath("web.config"));

它有效,在我的解决方案中,它没问题......