0

免责声明:我对 ASP.NET 还很陌生,所以我在进行过程中会弄清楚。

我正在尝试在网页本身的页面后面的代码中输出函数的结果。

例子:

代码背后:

public string POSTResult(string e) { ... return TheResult; }

ASPX 页面:

Output is: <%=POSTResult("argument")%>

但是,加载页面错误,说“当前上下文中不存在名称'POSTResult'。”

显然,我对如何从 ASPX 页面访问代码隐藏页面的方式有些偏离。我的 ASPX 页面顶部有这个:

<%@ Page Title="" Language="C#" MasterPageFile="~/master/default.Master" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="Bitfork.login" %>

列出的 CodeBehind 值是代码隐藏页面的名称。

预计到达时间:

我背后的代码内容(login.aspx.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;

namespace Bitfork.master
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public string POSTResult(string e)
        {
            // variables to store parameter values
            string url = "https://accounts.google.com/o/oauth2/token";

            // creates the post data for the POST request
            string postData = (e);

            // create the POST request
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = postData.Length;

            // POST the data
            using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
            {
                requestWriter2.Write(postData);
            }

            //  This actually does the request and gets the response back
            HttpWebResponse resp = (HttpWebResponse)webRequest.GetResponse();

            string responseData = string.Empty;

            using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
            {
                // dumps the HTML from the response into a string variable
                responseData = responseReader.ReadToEnd();
            }

            return responseData;
        }
    }
}

传递的参数示例,API 密钥已编辑:

code=[redacted]&client_id=[redacted]&client_secret=[redacted]&redirect_uri=http://localhost:60284/login.aspx&grant_type=authorization_code

ETA2:

我不知道它是否相关,但似乎我的页面背后的代码和我的网页根本没有相互通信。例如,我无法在我的代码隐藏页面中通过 ID 访问 DIV 来更改它们的内容。

4

0 回答 0