Link to home
Start Free TrialLog in
Avatar of YZlat
YZlatFlag for United States of America

asked on

The requested resource does not support http method 'GET'

I have a Web API where I have more than one get method in the controller. One of the controller methods returns true or flas - used as a validation of employee id

Below is my code:

My repository class:

    public class myRepository
    {
    
    	public myClasses.Type[] GetAllTypes()
    	{
    
    		return new myClasses.Type[]
    		{
    			new myClasses.Type 
    			{
    				typeId="1",
    				typeVal = "New"
    			},
    			new myClasses.Type 
    			{
    				typeId="2",
    				typeVal = "Old"
    			}
    	   };
    
    	}
    
    	public myClasses.Employee[] GetAllEmployees()
    	{
    
    		return new myClasses.Employee[]
    		{
    			new myClasses.Employee 
    			{
    				empId="111111",
    				empFName = "Jane",
    				empLName="Doe"
    			},
    			new myClasses.Employee 
    			{
    				empId="222222",
    				empFName = "John",
    				empLName="Doe"
    			}
    	   };
       
    	}
    
    	public bool VerifyEmployeeId(string id)
    	{
    
    		myClasses.Employee[] emp = new myClasses.Employee[]
    		{
    			new myClasses.Employee 
    			{
    				empId="111111",
    				empFName = "Jane",
    				empLName="Doe"
    			},
    			new myClasses.Employee 
    			{
    				empId="222222",
    				empFName = "John",
    				empLName="Doe"
    			}
    	   };
    
    		for (var i = 0; i <= emp.Length - 1; i++)
    		{
    			if (emp[i].empId == id)
    				return true;
    		}
    		return false;
    	}
    }

Open in new window

and my model class:

    public class myClasses
    {

        public class Employee
        {
            public string empId { get; set; }
            public string empFName { get; set; }
            public string empLName { get; set; }
        }

        public class Type
        {
            public string typeId { get; set; }
            public string typeVal { get; set; }
        }
    }

Open in new window

and here is my controller:

	public class myClassesController : ApiController
    {
		private myRepository empRepository;

        public myClassesController()
        {
            this.empRepository = new myRepository();
        }
        
        public myClasses.Type[] GetTypes()
        {
            return empRepository.GetAllTypes();
        }

        public myClasses.Employee[] GetEmployees()
        {
            return empRepository.GetAllEmployees();
        }

		public bool VerifyEmployee(string id)
        {
            return empRepository.VerifyEmployeeId(string id);
        }
    }

Open in new window

Everything compiles fine but when I run it using

    http://localhost:49358/api/myClasses/GetTypes

Open in new window


and that returns all the data in xml format. But when I run

    http://localhost:49358/api/myClasses/VerifyEmployee/222222

Open in new window


I get an error "The requested resource does not support http method 'GET'"



My WebAPIConfig:

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional });

            // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
            // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
            // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
            //config.EnableQuerySupport();

            // To disable tracing in your application, please comment out or remove the following line of code
            // For more information, refer to: http://www.asp.net/web-api
            config.EnableSystemDiagnosticsTracing();
        }
    }

Open in new window


Can anyone point me in the right direction? What am I doing wrong here? Are there better ways to do validations against data via Web API?
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial