简体   繁体   中英

Unable to read value from Web.Config

I'm trying to implement a recaptcha on a login page. 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)

Is there a setting that needs to be done?Below are my codes.Hope to get a reply soon.

aspx file

<%@ 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

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

web.config file

 <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. See the MSDN article for info: https://msdn.microsoft.com/en-us/library/610xe886%28v=vs.140%29.aspx

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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