简体   繁体   中英

Displaying C# variable on ASP.NET webpage

I am designing a website in ASP.NET, and I am integrating the Steam API into it. I am using DotNetOpenAuth for the authentication. I am confused as to how I am to access and display the variable responseURI (Line 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.

Here is my 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. The Page is the class, just make it a class-level value. (I believe the visibility needs to be at least protected , since the displayed page "inherits" from the code-behind page.)

protected string ResponseURI { get; set; }

That would be placed at the class-level, like any other property in 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%>

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