Unable to pass parameter to the Controller in Visual Studio (2019 v)

  Uncategorised

I am currently unable to pass the parameter from a method I created to the Controller. When I run it manually with the parameter in postman, everything works OK.

How do I pass the parameter to the Controller? Is there any way to set this up to automatically route instead of creating individuals routes based on different parameters? It’s a very basic application.

Error Message:
{"Message":"No HTTP resource was found that matches the request URI ‘https://localhost:44364/api/WebInterface/RemoveNurse’.","MessageDetail":"No action was found on the controller ‘WebInterface’ that matches the request."}

CONTROLLER:

using WebInterfaceAPI.Helpers;
using WebInterfaceAPI.Services;
using WebInterfaceAPI.Models;
using Newtonsoft.Json;
using System.Data.SqlClient;
using System.Web.Http;
using System.Net;

namespace WebInterfaceAPI.Controllers
{
    public class WebInterfaceController : ApiController
    {
        private NurseService NurseService { get; set; }

        public WebInterfaceController()
        {
            NurseService = new NurseService();
        }

        #region Nurse Actions
        /// <summary>
        /// Removes specified nurse
        /// </summary>
        /// <param name="nurseID"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult RemoveNurse(int id)
        {
            APIResponseModel result = NurseService.removeNurse(id);
            if (result.IsSuccess)
            {
                return Ok(result.Message);

            }
            else
            {
                return Content(HttpStatusCode.BadRequest, result.Message);
            }
        }

        #endregion
    }
};

RouteConfig.cs

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { 
                action = "Index", 
                id = UrlParameter.Optional 
            }
        );
    }
}

METHOD: (NurseService.cs)

public APIResponseModel removeNurse(int nurseID)
        {
            using (var dbContext = new DataBase())
            {
                OnCallNurses_Nurses rowToDelete = (from nurse in dbContext.OnCallNurses_Nurses
                                                   where nurse.id == nurseID
                                                   select nurse).SingleOrDefault();

                if (rowToDelete == null)
                {
                    return new APIResponseModel(false, "Cannot delete nurse.");
                }
                else
                {
                    dbContext.OnCallNurses_Nurses.Remove(rowToDelete);
                    dbContext.SaveChanges();
                    return new APIResponseModel(true, "Nurse successfully removed.");
                }
            }
        }

FRONT-END

function removeNurse(nID) {

$('#loader').removeClass('d-none');
var webMethod = "https://localhost:44364/api/WebInterface/RemoveNurse";
// var webMethod = "https://web..com/api/api/WebInterface/RemoveNurse";

$.ajax({
    type: "POST",
    url: webMethod,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: {
        nurseID: nID
    },
    success: function (data) {
        alert(data);
        // $(nurseRow).remove();
        $('#loader').addClass('d-none');

    },
    error: function (xhr, status) {
        console.log(xhr.responseText);
        console.log(xhr.status);
    }
});
};

Source: Visual Studio Questions

LEAVE A COMMENT