When I added HttpPost attribute my app can not find the correct route

79 Views Asked by At

I used ASP.NET 6 to develop A Web Application . I'm faced with some strange error .When I add an HttpPost attribute on action methods, the routing system fails to find the specific view. I added custom routing and also configured the routing system in Program.cs file . However, I still get http response 405 error . When I change the http request to GET every things work fine. Can anyone help me with this matter?

1

There are 1 best solutions below

1
Qing Guo On

Wehn I add HttpPost attribute on action methods , the routing system failed to find the specific view However , I still get http request 405 error . When I change http reguest to get every things work fine

I guess you excute the HttpPost method by url request. The url request, which is a HTTP GET command. So, your app got an HTTP GET command but your method wants to accept an HTTP POST method. That's why it gets http request 405 error.

You can excute Get method firstly, then call the Post method. From the below code you can see in the HttpPost method you can find the specific view.

 public IActionResult Index()
 { 
        //return View();
     return Indexpost();
 }

[HttpPost]
 public IActionResult Indexpost()
 {
        return View("Privacy");
 }

result: enter image description here