简体   繁体   中英

How to display Hello when a button is clicked

Requires method overloading, but I don't need to accept parameters, how can I make it work?

public IActionResult SlMas()
            {
                return View();
            }
            [HttpPost]
            public string SlMas()
            {
                    return "hello";
            }
    
    
    
    @{
        ViewData["Title"] = "SlMas";
    }
    
    <form method="post">
        <p>
            <input type="submit" value="Проверить" />
        </p>
    </form>
public IActionResult SlMas()
            {
                return View();
            }
            [HttpPost]
            public string SlMas(int a)
            {
                    return "hello";
            }

you can do something like this however, I would not recommend this. You should either tackle this through front end or send something useful in the post request.

Method overloading is multiple methods can have the same name with different parameters .

If you do not want parameters in HttpPost SlMas method,you must change the name.

For your scenario,you could use ActionName attribute when you want to alias the name of the Action method:

public IActionResult SlMas()
{
    return View();
}
[HttpPost, ActionName("SlMas")]
public string Hello()
{
    return "hello";
}

Besides,as @Martin Costello said,you could also use Route attribute:

[HttpPost]
[Route("/TheSameWithYourHttpGetUrl")]
public string Hello()
{
    return "hello";
}

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