skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    The fix was pieced together from different posts.

    1. rename/delete VS auto generated cache files MVC-ControllerTypeCache.xml and MVC-AreaRegistrationTypeCache.xml
    2. run VS in "release" mode to regenerate cache files.

    It helped using Postman to test URLs.


  2. Try these steps:

    1. Check the controller code: Ensure that the controller method you are trying to access is properly defined and accessible. Make sure that the controller method has the correct signature, including the correct HTTP verb (GET, POST, etc.).

    2. Check the routing: Ensure that the routing configuration in your web application is correct and that it properly maps the URL to the appropriate controller method. Verify that the URL being used in your AJAX call matches the routing configuration.

    3. Check the AJAX call: Verify that the AJAX call being made from the JS file is properly formatted and includes the correct URL for the controller method. Use a debugging tool such as the browser console to view any errors that may be occurring during the AJAX call.

    4. Check for authentication and authorization: Ensure that the user making the AJAX call has the necessary permissions to access the controller method. Check the authentication and authorization settings in your web application to ensure that they are properly configured.

    5. Check for any custom middleware: If your web application has any custom middleware, ensure that it is not blocking the AJAX call. Check the middleware configuration and verify that it is not interfering with the AJAX call.

    6. Hopefully, these suggestions will help you diagnose and resolve the issue. If you continue to have trouble, feel free to provide more details or code snippets, and we can try to provide further assistance.

    Have a good one!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search