Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a httppost getting same parameters from httpget?

I have a controller to show up a model (User) and want to create a screen just with a button to activate. I don't want fields in the form. I already have the id in the url. How can I accomplish this?

like image 283
waldecir Avatar asked Dec 13 '10 13:12

waldecir


People also ask

Can we use HttpPost instead of HttpGet?

HTTPGet method is default whereas you need to specify HTTPPost attribute if you are posting data using HTTPPost method. 2. HTTPGet method creates a query string of the name-value pair whereas HTTPPost method passes the name and value pairs in the body of the HTTP request.

Can we have two methods with same name in controller?

To answer your specific question, you cannot have two methods with the same name and the same arguments in a single class; using the HttpGet and HttpPost attributes doesn't distinguish the methods.

Can we use get and post in same method?

You can use the same method for Get and Post without specifying anything and it will work smooth. If you are using [HttpGet] or [HttpPost] then remove that and use for get and post and it will work.

What is HttpGet and HttpPost in C#?

HttpGet and HttpPost are both the methods of posting client data or form data to the server. HTTP is a HyperText Transfer Protocol that is designed to send and receive the data between client and server using web pages.


1 Answers

Use [ActionName] attribute - this way you can have the URLs seemingly point to the same location but perform different actions depending on the HTTP method:

[ActionName("Index"), HttpGet]
public ActionResult IndexGet(int id) { ... }

[ActionName("Index"), HttpPost]
public ActionResult IndexPost(int id) { ... }

Alternatively you can check the HTTP method in code:

public ActionResult Index(int id)
{
    if (string.Equals(this.HttpContext.Request.HttpMethod, "POST", StringComparison.OrdinalIgnoreCase))
    { ... }
}
like image 82
Knaģis Avatar answered Nov 01 '22 19:11

Knaģis