[英]Unable to read value from Web.Config
我正在嘗試在登錄頁面上實現recaptcha。 但每次遇到代碼時,都會發生空引用異常。 我正在使用帶有默認登錄模板的asp.net,它有自己的Web.Config(雖然不知道為什么)
是否需要進行設置?以下是我的代碼。希望盡快得到回復。
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文件
recaptcha.PublicKey = System.Configuration.ConfigurationManager.AppSettings["reCAPTCHAPublicKey"].ToString();
recaptcha.PrivateKey = System.Configuration.ConfigurationManager.AppSettings["reCAPTCHAPrivateKey"].ToString();
web.config文件
<appSettings>
<add key="reCAPTCHAPublicKey" value="fromgooglecaptchakey"/>
<add key="reCAPTCHAPrivateKey" value="fromgooglecaptchakey"/>
</appSettings>
嘗試這個,它適用於我的代碼:
using System.Web.Configuration;
/* ... */
WebConfigurationManager.AppSettings["reCAPTCHAPublicKey"];
你需要確保在你的asp.net項目的后端某處打開web配置。 有關信息,請參閱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.