Error adding Httppost to controller

Asked

Viewed 2,030 times

1

I’m having a problem adding the attribute [HttpPost] on my controller. I mean, I can add the attribute without problems, but when I compile and go to the registration form I just can’t access.

I get the following error:

Server Error in Application '/'.

Unable to find resource.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could not be removed, its name has been changed or is temporarily unavailable. Scan the URL and make sure it’s typed correctly.

Requested URL: /user/add

Version Information: Microsoft . NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.33440

Also, when I remove the attribute httppost, I can see the registration form, but I can’t do the insert in the bank.

1 answer

2


When you add the Httppost attribute to an action. It can only be accessed via the HTTP protocol POST method.

If your intention is only to display the form. Do not decorate your method with any attribute, the default will be GET.

What you can do is define an Add method without any attribute. This method will be accessed via GET and display the form.

Then you create a second method, for example Add-on, decorated with the attributes Httppost and Actionname="Add"

The methods can without defined as follows

public ActionResult Adicionar() 
{ 
}

[HttpPost]
[ActionName="Adicionar"]
public ActionResult AdicionarConfirmado(Cliente cliente) 
{ 
}

Already your form that will save the record would look like this:

<form action="Adicionar" method="post"></form>
  • public Actionresult Add() { } ?

  • I got it. That’s right, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.