2023-04-18

Can't access controller method in Web Application

I have inherited an old Web application.

Simple Background: Currently, there are buttons on razor .cshtml page that call JS functions on a separate file. It does ajax calls to controller methods via SweetAlert just fine.

.cshtml file:

<div class="col-sm-1"> @*<input id="EmpEmailButton" class="btn btn-sm " type="button" value="Link" title="Link email to a user with Email address." onclick="updateDatabase()" /> </div>

.js file:

function onDeleteButtonClick(e) {

  swal({
    title: "Are you sure?",
    text: "You will update this user!",
    type: "warning",
    showCancelButton: true,
    confirmButtonColor: "#DD6B55",
    confirmButtonText: "Yes, updated it!",
    closeOnConfirm: false
  }, function () {
    var id = $(e).attr('data-value')
    $.ajax({
      type: "POST",
      url: '/admin/UpdateUser?userid=' + id
    });
    swal("Updated!", "User has been updated.", "success");
    document.location.reload();
  });

}

c# file:

public string UpdateUser(int id = 0)
        {
            // db stuff

            return "Success";

        }

What I am trying to do: I am trying to create new JS functions that call new Controller methods using same style as existing approach.

Changes I have made:

  1. Created new Controller methods by copying and pasting and just changing method name and method code.
  2. I have used copied and pasted same JS code and just updated URL to call new method in Controller.

Results: It fails every time with 404 when testing new method(s) I create.

I have also tested using Postman. Postman returns "200 OK" for existing methods in Controller. And 404 for all methods I have created.

Things I have tried:

  1. I have read that Controllers sometimes have issues and to create a brand new controller and test. I have, same results.
  2. I have done a "clean and build" in VS. No diff.
  3. I am using routing defined in RouteConfig.cs. No diff.
  4. Switched return type from string to ActionResult. No diff.
  5. Added attribute to method: [System.Web.Mvc.AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]. No diff.
  6. I have researched if it is possible to block Controller methods (maybe the last guy wanted to control access??). But nothing seems out of the ordinary. I did get excited when I discovered "MVC-AreaRegistrationTypeCache.xml" thinking there was a file that may have list of specific allowed methods to access but this was not helpful.
  7. Tested using Postman. 404 every time. Postman Response
  8. Cleared my browser cache. No diff.
  9. Followed ChatGPT solution. No diff.
  10. Followed GitHub CoPilot hint solution. No diff.

ANY HELP WOULD BE APPRECIATED!!

I have read that Controllers sometimes have issues and to create a brand new controller and test. I have, same results.

I have done a "clean and build" in VS. No diff.

I am using routing defined in RouteConfig.cs. No diff.

I have researched if it is possible to block Controller methods (maybe the last guy wanted to control access??). But nothing seems out of the ordinary. I did get excited when I discovered "MVC-AreaRegistrationTypeCache.xml" thinking there was a file that may have list of specific allowed methods to access but this was not helpful.

Tested using Postman. 404 every time.



No comments:

Post a Comment