[英]Set facebook application web.config settings in code (C#)
I'm trying to set the web.config settings in a facebook app from code to avoid working directly with the web.config file. 我正在尝试从代码中设置facebook应用程序中的web.config设置,以避免直接使用web.config文件。 I've tried a custom ConfigurationSection class, and then using the WebConfigurationManager to reach the web.config file. 我尝试了自定义ConfigurationSection类,然后使用WebConfigurationManager访问web.config文件。 The problem is that I can't get an instance of the Configuration object. 问题是我无法获得Configuration对象的实例。 This is my 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()
{
}
}
And the page that uses this: 以及使用它的页面:
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();
}
The web.config looks like this (the part I'm trying to work with): 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 = "" />
Doing this, gives me the "Object reference not set to an instance of an object." 这样做,给我“对象引用未设置为对象的实例”。 on _config, which tells me that nothing gets returned. 在_config上,它告诉我没有返回任何内容。
Is there anything "facebook specific" that causes this? 是否有任何“facebook特定”导致这种情况?
Another thing; 另一件事; I came across this new method of working with facebook settings in code: 我在代码中遇到了使用facebook设置的这种新方法:
FacebookContext.SetApplication( IFacebookApplication )
I haven't been able to find a good example that uses this. 我无法找到一个使用它的好例子。 Has anyone worked with this before? 以前有人有过这个吗?
Just use 只是用
var sec = ConfigurationManager.GetSection("facebookSettings");
FacebookConfigurationSection config = (sec as Facebook.FacebookConfigurationSection);
config.AppID etc etc config.AppID等
Try 尝试
section type="Facebook.FacebookConfigurationSection"
or, if you have no namespace 或者,如果您没有命名空间
section type="FacebookConfigurationSection"
I assume the line that you commented out didn't work either? 我假设你注释掉的那条线也不起作用?
FacebookConfigurationSection config = (FacebookConfigurationSection)System.Configuration.ConfigurationManager.GetSection("facebookSettings");
Maybe the problem is with your <facebookSettings>
section try it in following format: 也许问题出在您的<facebookSettings>
部分,请按以下格式进行尝试:
<facebookSettings>
<add key="appId " value="xxxxxxxxxxxxxxxx" />
<add key="appSecret " value="xxxxxxxxxxxxxxxxxxxxxxxxxxx" />
...
...
<facebookSettings>
I am not entirely sure why your code does not work, but try the below, I've tried a sample with your web.config file and it does work 我不完全确定为什么你的代码不起作用,但尝试下面的内容,我已经尝试了一个带有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();
Well, I couldn't find any good examples on using 好吧,我找不到任何关于使用的好例子
FacebookContext.SetApplication( IFacebookApplication )
and I'm not sure it's even possible to do it the "ConfigurationSection" way... 而且我不确定它甚至可以用“ConfigurationSection”的方式来做...
So, I cheated.. 所以,我骗了..
I loaded the web.config file into an XmlDocument object and manipulated it that way... 我将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"));
It works, and in my solution, it's ok... 它有效,在我的解决方案中,它没关系......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.