简体   繁体   English

无法从Web.Config读取值

[英]Unable to read value from Web.Config

I'm trying to implement a recaptcha on a login page. 我正在尝试在登录页面上实现recaptcha。 But every time it hits the code , a null reference exception happens. 但每次遇到代码时,都会发生空引用异常。 I'm using asp.net with the default login template, it has its own Web.Config(not sure why though) 我正在使用带有默认登录模板的asp.net,它有自己的Web.Config(虽然不知道为什么)

Is there a setting that needs to be done?Below are my codes.Hope to get a reply soon. 是否需要进行设置?以下是我的代码。希望尽快得到回复。

aspx file aspx文件

<%@ Page Title="Log In" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"CodeBehind="Login.aspx.cs" Inherits="Penerangan.Emedia2u.Web.Account.Login" %>
<%@ Register TagPrefix="recaptcha" Namespace="Recaptcha" Assembly="Recaptcha" %>

<asp:Label Visible="false" ID="lblResult" runat="server" />
                    <recaptcha:RecaptchaControl ID="recaptcha" runat="server" />

.cs file .cs文件

  recaptcha.PublicKey = System.Configuration.ConfigurationManager.AppSettings["reCAPTCHAPublicKey"].ToString();
                recaptcha.PrivateKey = System.Configuration.ConfigurationManager.AppSettings["reCAPTCHAPrivateKey"].ToString(); 

web.config file web.config文件

 <appSettings>
    <add key="reCAPTCHAPublicKey" value="fromgooglecaptchakey"/>
    <add key="reCAPTCHAPrivateKey" value="fromgooglecaptchakey"/>
  </appSettings>

Try this one, it works on my code: 尝试这个,它适用于我的代码:

using System.Web.Configuration;

/* ... */

WebConfigurationManager.AppSettings["reCAPTCHAPublicKey"];

you'll need to make sure the web config is opened somewhere on the backend of your asp.net project. 你需要确保在你的asp.net项目的后端某处打开web配置。 See the MSDN article for info: https://msdn.microsoft.com/en-us/library/610xe886%28v=vs.140%29.aspx 有关信息,请参阅MSDN文章: https//msdn.microsoft.com/en-us/library/610xe886%28v=vs.140%29.aspx

System.Configuration.Configuration rootWebConfig1 =
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM