-1

My route is correctly configured as I already saw it in another questions. Web API uses MapHttpRoute, and it uses System.Web.Http. I decorated my Actions with [System.Web.Http.HttpPost] but it seems not to work and it returns the error message:

The requested resource does not support http method 'GET'

I tried this solution [System.Web.Http.AcceptVerbs("GET", "POST")] as I see here on the same question. The requested resource does not support HTTP method 'GET' and it worked.

But on the API Help Page, this is what I seeRed Circle

the METHOD of the Action is GET that that should be POST.

Maybe I am missing something that should not or should be implemented on the Action I am working with.

Here is my code in the Controller.

    [HttpPost, Route("DestroySession/{userID}", Name = "DestroySession"), AcceptVerbs("GET" , "POST")]
    public async Task<IHttpActionResult> DestroyUserSession(string userID)
    {
        SystemResult systemResult = new SystemResult();
        await Task.Run(() => {
            IAbstractLogic<UserInput, SystemResult> systemProcess = new LogoutLogic();
            UserInput userInput = new UserInput
            {
                UserID = userID
            };
            systemResult = systemProcess.doProcess(userInput);
        }).ConfigureAwait(false);
        return Content(HttpStatusCode.OK, new 
        { 
            message = systemResult.ResultMessage, status = systemResult.ResultCode == 0 
        });
    }

And here is my WebApiConfig

       config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

Any help would be much appreciated. Regards

Levesque Xylia
  • 349
  • 3
  • 16

1 Answers1

-1

Whooosh! It seems that my code is working very fine. I just tested it in POSTMAN and it literally works. All I can say is that if your request is to POST something. you need a third party application to test it out. Testing it on the browser alone gives you so much problem.

Levesque Xylia
  • 349
  • 3
  • 16
  • 1
    How were you "testing it in the browser"? Your question contains no detail on that. If you were typing the url host.com/api/users/destroysession/1 then that will have been doing a GET. POST needs to be done by script or a form on the page. You can look in the network tab of the dev tools to see what request was issued. Not that I expect asp would have lied about it.. – Caius Jard Oct 23 '21 at 06:17
  • @CaiusJard, just putting the values of the arguments on the URL. – Levesque Xylia Oct 23 '21 at 06:19
  • 1
    Yeah, that always does a GET – Caius Jard Oct 23 '21 at 06:20
  • OOoooow...!!!! Now I get it. Thank you Sir. @CaiusJard – Levesque Xylia Oct 23 '21 at 06:20
  • As an aside, you should maybe DELETE, because the request doesn't contain any body data. Typically we use POST or PATCH for updates, DELETE for deletes, PUT for inserts. It is best to try and keep GET for read only accesses that don't change anything. There was some thing a while ago where (google?) thought it would be a good idea to prefetch every GETtable thing on a page to speed up browsing "in case that thing was clicked", which caused problems for all those apps where GET modified data.. – Caius Jard Oct 23 '21 at 06:22
  • @CaiusJard , Should I use `GET` when I request for the updating of the data? – Levesque Xylia Oct 23 '21 at 06:24
  • Er, no i think as per above it is sensible to keep a policy of using GET only for read-only things that do not perform updates that would change what is returned next time GET is called. By all means update some "last accessed on" date in a GET, but don't go editing the user name or whatever.. strive for following that convention of POST = update, PUT = insert, DELETE = delete, GET = select – Caius Jard Oct 23 '21 at 06:26