locked
Can't you use the keyword 'base' in a webmethod in .Net RRS feed

  • Question

  • User-2004582644 posted

    Hello,

    In masterpage c# I have set this public void:

    public void mtwelcomeuser()
    {
        //Code to set instance of masterpage...
    }

    I need recovery the instance set in masterpage on webmethod:

    [WebMethod(EnableSession = true)]
    [ScriptMethod]
    public static void Savep(p tlco)
    {
       //recovery the instance set in masterpage
    }

    I'm trying to use the base keyword in a webmethod, but the compiler won't allow me to use it.

    Why not?

    No solution?

    [WebMethod(EnableSession = true)]
    [ScriptMethod]
    public static void Savep(p tlco)
    {
       ((Masterpage)base.Master).mtwelcomeuser();
    }

    Sunday, April 12, 2020 4:59 PM

Answers

  • User753101303 posted

    You can use System.Web.HttpContext.Current.User to retrieve the user from the current http rrequest (it is exposed on Pages for convenience by using that as well).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 12, 2020 7:08 PM

All replies

  • User753101303 posted

    Hi,

    See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static

    A static method belongs to the type (and is called using <TypeName>.MyMethod rather than <objectInstance>.MyMethod). And so trying to use base or to call an instance method from a static method won't work.

    Not sure what mtwelcomeuser() is supposed to do (and you are not even using tlco ?)

    Sunday, April 12, 2020 6:23 PM
  • User-2004582644 posted

    Not sure what mtwelcomeuser() is supposed to do (and you are not even using tlco ?)
    

    Thank you for reply.

    On mtwelcomeuser() :

            string username = Page.User.Identity.Name.ToLower();

    I need recovery the value of string username on  webmethod tlco:

    [WebMethod(EnableSession = true)]
    [ScriptMethod]
    public static void Savep(p tlco)
    {
       ((Masterpage)base.Master).mtwelcomeuser();
    }

    Because the username may be unauthorized to use webmethod tlco

    The webmethod tlco write on database and not all users must be authorized.

    Any suggestions?

    Sunday, April 12, 2020 6:59 PM
  • User753101303 posted

    You can use System.Web.HttpContext.Current.User to retrieve the user from the current http rrequest (it is exposed on Pages for convenience by using that as well).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, April 12, 2020 7:08 PM
  • User-2004582644 posted

    You can use System.Web.HttpContext.Current.User to retrieve the user from the current http rrequest (it is exposed on Pages for convenience by using that as well).

    Thank you for help, really appreciated.

    Monday, April 13, 2020 7:17 AM