简体   繁体   English

在ASP.NET网页上显示C#变量

[英]Displaying C# variable on ASP.NET webpage

I am designing a website in ASP.NET, and I am integrating the Steam API into it. 我正在ASP.NET中设计一个网站,并将Steam API集成到其中。 I am using DotNetOpenAuth for the authentication. 我正在使用DotNetOpenAuth进行身份验证。 I am confused as to how I am to access and display the variable responseURI (Line 22). 我对如何访问和显示变量responseURI (第22行)感到困惑。 Whenever I try to display the variable on the webpage using this: 每当我尝试使用此方法在网页上显示变量时:

<p><%=responseURI%></p>

I get a message stating: 我收到一条消息,指出:

The name 'responseURI' does not exist in the current context. 名称“ responseURI”在当前上下文中不存在。

Here is my C# 这是我的C#

using DotNetOpenAuth.OpenId.RelyingParty;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class loginPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var openid = new OpenIdRelyingParty();
        var response = openid.GetResponse();

        if (response != null)
        {
            switch (response.Status)
            {
                case AuthenticationStatus.Authenticated:
                    // do success
                    var responseURI = response.ClaimedIdentifier.ToString();
                    break;

                case AuthenticationStatus.Canceled:
                case AuthenticationStatus.Failed:
                    // do fail
                    break;
            }
        }
        else
        {
            using (OpenIdRelyingParty openidd = new OpenIdRelyingParty())
            {
                IAuthenticationRequest request = openidd.CreateRequest("http://steamcommunity.com/openid");
                request.RedirectToProvider();
            }
        }
    }
}

Because it doesn't exist in the page's context, only in the context of that switch case. 因为它在页面的上下文中不存在,所以仅在该switch条件的上下文中。 The Page is the class, just make it a class-level value. Page是类,只需使其成为类级别的值即可。 (I believe the visibility needs to be at least protected , since the displayed page "inherits" from the code-behind page.) (我认为可见性至少需要protected ,因为显示的页面是从代码隐藏页面“继承”的。)

protected string ResponseURI { get; set; }

That would be placed at the class-level, like any other property in C#. 它将像C#中的任何其他属性一样放在类级别。 For example: 例如:

protected string ResponseURI { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
    //...
}

Then set that in your code instead: 然后在您的代码中进行设置:

this.ResponseURI = response.ClaimedIdentifier.ToString();

And display it on the page: 并将其显示在页面上:

<%=ResponseURI%>

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

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