Same url works sometimes yes and sometimes no

Asked

Viewed 48 times

-1

I am with a web form, in which I have this code:

<form action="../../MenuPrincipal" method="POST" class="form-inline">

<button type="submit">
</form>

and my file structure is like this:

inserir a descrição da imagem aqui

My form is in the file pesquisasSp1Pesquisar.cshtml

The problem is this... When I do Submit, it goes to the correct url, but with this error:

inserir a descrição da imagem aqui

I know the url is correct because if I click there and hit enter, reloading, it opens correctly:

inserir a descrição da imagem aqui

OBS: It doesn’t work if I press F5.

To show you better what’s going on:

https://drive.google.com/file/d/1DHWIXroacGBZI-g3NDxbqZdUjleNagcS/view?usp=sharing


EDIT1: At Alisson’s request, I changed my action to Sp1Psearch in order to call the Onpost that is on this page. Result:

inserir a descrição da imagem aqui

Code:

  • 1

    Apparently the problem is you are giving POST on the Menu/Menuprincipal route and it has only predicted the GET.

  • @Alissonmarqui. I have the get of the page done and regarding my post I have just this: public IActionResult OnPost(){ return Page();}. It’s wrong for me to leave the form action like that?

  • You really will make a POST of some information, or just want to redirect from one page to another ? This Onpost is on Menuprincipal ?

  • I will do the post some information yes... The Onpost is in the file . Cs of the page searchSp1Psearch

  • In your form you are pointing to a Post on the route of Menuprincipal, then this Onpost should stay there.

  • then in my onpost, I must make a redirect so I can get back to Menuprincipal?

  • if I remove the "action" from the form and put in my onpost a Response.Redirect("../../MenuPrincipal") so in theory it was supposed to work?

Show 3 more comments

1 answer

0


Your form is doing a POST in the Menuprincipal file, this file does not have an Onpost method for this reason you get the 400 error. When using F5 the browser tries to redo the last action, in case it is trying to resend the POST so the error persists, when ordering the browser to reload the page as you did in the video, the browser requests the GET on the route, so it works.

According to the information that you passed in the comments, the POST method that you want to trigger is in.Cs searches, such as the view where the form is in the same file. Cs you can take the action from the form so it will fetch in that same file by Onpost.

In Onpost you will process the data and make a redirect to Menuprincipal’s GET, something like Response.Redirect("../../MenuPrincipal") as this it will return the view correctly.


EDIT

As discussed in chat with Lima, we found that we also had to adjust the view @Model to point to the file . correct Cs.

  • That’s what I deduced, by your explanation in the comments... I removed the action and put the Response.redirect on onpost, but it seems that just reloads the form page, no longer gives the 400 error, but it does not go to the route, just reload the form page. I tried to debug the code inside the onpost, but it seems like it’s not even going through there

  • @File in Form action points to the Onpost of the searchSp1Psearch, for example, action='/pesquisa/pesquisasSp1Pesquisar'

  • I will leave an Edit with the print that appeared. Again it did not debug

Browser other questions tagged

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