Legen Sie facebook-Anwendung web.config-Einstellungen in code (C#)
Ich versuche mit dem web.config Einstellungen in einer facebook-app von code zu vermeiden, arbeiten Sie direkt mit dem web.config-Datei.
Ich habe versucht, einen benutzerdefinierten ConfigurationSection-Klasse, und verwenden Sie dann die WebConfigurationManager zu erreichen das web.config-Datei. Das problem ist, dass ich nicht bekommen kann eine Instanz von Configuration-Objekt. Das ist mein code:
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()
{
}
}
Und die Seite, die verwendet diese:
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 sieht so aus (der Teil, den ich versuche, mit zu arbeiten):
<configSections>
<section type="Facebook.FacebookConfigurationSection, Facebook" name="facebookSettings" allowLocation="true" allowDefinition="Everywhere"/>
</configSections>
<facebookSettings
appId = "xxxxxxxxxxxxxxx"
appSecret = "xxxxxxxxxxxxxxxxxxxxxxxxx"
canvasPage = "xxxxxxxxxxxxxxxxxx"
canvasUrl ="xxxxxxxxxxxxxxxxxx"
cancelUrlPath = "" />
Dies zu tun, gibt mir ein "Object reference not set to an instance of an object." auf _config, die mir sagt, dass nichts zurückgegeben wird.
Ist es etwas, das "facebook-spezifische", dass dies die Ursache?
Andere Sache; ich kam in diese neue Methode der Arbeit mit facebook-Einstellungen in code:
FacebookContext.SetApplication( IFacebookApplication )
Ich habe nicht in der Lage zu finden, ein gutes Beispiel für die Verwendung dieser.
Hat jemand arbeitete mit diesem vor?
Du musst angemeldet sein, um einen Kommentar abzugeben.
Verwenden Sie einfach
config.AppID etc etc
Versuchen
oder, wenn Sie keinen namespace
Ich nehme an, die Zeile, die du auskommentiert hat nicht funktioniert entweder?
Ich bin mir nicht ganz sicher, warum dein code nicht funktioniert, aber versuchen Sie, die unten, ich habe versucht, eine Probe mit Ihrer Website.config-Datei und es funktioniert
Vielleicht ist das problem mit Ihrem
<facebookSettings>
Abschnitt versuchen Sie es im folgenden format:Gut, ich konnte keine guten Beispiele für die Verwendung
und ich bin nicht sicher, es ist sogar möglich, es zu tun die "ConfigurationSection" Weg...
So, ich habe geschummelt..
Lud ich die web.config-Datei in ein XmlDocument-Objekt und bearbeitet es so...
Funktioniert es, und in meiner Lösung, es ist ok...