none
405 method not allowed(postman) RRS feed

  • Question

  • Hi All,

       I have created delete,put method using WebAPI. While calling the method from postman i am getting the method not allowed. I have done R&D and applied the changes but no luck. Please help me on this.

     //[System.Web.Http.AcceptVerbs("GET", "POST","DELETE")]
            //[System.Web.Http.HttpDelete]
            public void Delete(int id)
            {
                using (EmployeeDBEntities entities = new EmployeeDBEntities())
                {
                    entities.emps.Remove(entities.emps.FirstOrDefault(e=>id==e.id));
                    entities.SaveChanges();

                }

            }

    settings in webconfig

    <system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <modules runAllManagedModulesForAllRequests="true">
          <remove name="WebDAVModule" />
        </modules>
        <handlers>
          <remove name="WebDAV" />
          <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
          <remove name="OPTIONSVerbHandler" />
          <remove name="TRACEVerbHandler" />
          <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
        
        </handlers>
      </system.webServer>

    Saturday, March 28, 2020 3:14 PM

Answers

  • If you don't give a HTTP verb, then it's an implied 'Get'. On a 'Delete' named only WebAPI controller method, then you need the 'Delete' verb only. If you were  inserting or updating data, you would use Post or Put -- Put if the method name is 'Put' only. 

    ASP.NET WebAPI has a forum in ASP.NET forums.

    http://forums.asp.net/

    Also you should try to implement some kind of SoC where no direct data access is being done from a controller. 

    https://en.wikipedia.org/wiki/Separation_of_concerns

    using DAL;
    using Entities;
    using Microsoft.AspNetCore.Mvc;
    using System.Collections.Generic;
    
    namespace ProgMgmntCore2Api.Controllers
    {
        [Produces("application/json")]
        [Route("api/[controller]")]
        [ApiController]
    
        public class TaskController : ControllerBase
        {
            private readonly IDaoTask _daoTask;
    
            public TaskController(IDaoTask daoTask)
            {
                _daoTask = daoTask;
            }
    
            [HttpGet]
            [Route("GetTaskById")]
            public DtoTask GetTaskById(int id)
            {
                return _daoTask.GetTaskById(id);
            }
    
            [HttpGet]
            [Route("GetTasksByProjId")]
            public List<DtoTask> GetTasksByProjectId(int id)
            {
                return _daoTask.GetTasksByProjectId(id);
            }
    
            [HttpPost]
            [Route("CreateTask")]
            public void CreateTask(DtoTask dto)
            {
                _daoTask.CreateTask(dto);
            }
    
            [HttpPost]
            [Route("UpdateTask")]
            public void UpdateTask(DtoTask dto)
            {
                _daoTask.UpdateTask(dto);
            }
    
            [HttpPost]
            [Route("DeleteTask")]
            public void DeleteTask(DtoId dto)
            {
                _daoTask.DeleteTask(dto.Id);
            }
        }
    }

    Saturday, March 28, 2020 8:40 PM

All replies

  • If you don't give a HTTP verb, then it's an implied 'Get'. On a 'Delete' named only WebAPI controller method, then you need the 'Delete' verb only. If you were  inserting or updating data, you would use Post or Put -- Put if the method name is 'Put' only. 

    ASP.NET WebAPI has a forum in ASP.NET forums.

    http://forums.asp.net/

    Also you should try to implement some kind of SoC where no direct data access is being done from a controller. 

    https://en.wikipedia.org/wiki/Separation_of_concerns

    using DAL;
    using Entities;
    using Microsoft.AspNetCore.Mvc;
    using System.Collections.Generic;
    
    namespace ProgMgmntCore2Api.Controllers
    {
        [Produces("application/json")]
        [Route("api/[controller]")]
        [ApiController]
    
        public class TaskController : ControllerBase
        {
            private readonly IDaoTask _daoTask;
    
            public TaskController(IDaoTask daoTask)
            {
                _daoTask = daoTask;
            }
    
            [HttpGet]
            [Route("GetTaskById")]
            public DtoTask GetTaskById(int id)
            {
                return _daoTask.GetTaskById(id);
            }
    
            [HttpGet]
            [Route("GetTasksByProjId")]
            public List<DtoTask> GetTasksByProjectId(int id)
            {
                return _daoTask.GetTasksByProjectId(id);
            }
    
            [HttpPost]
            [Route("CreateTask")]
            public void CreateTask(DtoTask dto)
            {
                _daoTask.CreateTask(dto);
            }
    
            [HttpPost]
            [Route("UpdateTask")]
            public void UpdateTask(DtoTask dto)
            {
                _daoTask.UpdateTask(dto);
            }
    
            [HttpPost]
            [Route("DeleteTask")]
            public void DeleteTask(DtoId dto)
            {
                _daoTask.DeleteTask(dto.Id);
            }
        }
    }

    Saturday, March 28, 2020 8:40 PM
  • In your sample I think you meant [HttpDelete] for "DeleteTask" and HttpPost for Create and HttpPut for Update.

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles


    Sunday, March 29, 2020 2:37 AM
    Moderator
  • In your sample I think you meant [HttpDelete] for "DeleteTask" and HttpPost for Create and HttpPut for Update.

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles


    Nope, it works perfectly fine,  and one can use Post  for create, update or delete, becuase  of attribute routing. 

    Sunday, March 29, 2020 6:45 AM
  • Hi ,

      Thanks for your reply. I am getting the error for below lines.May be I am missing something. Can you clarify it please.

        [Produces("application/json")]
       
    [ApiController]

    Tuesday, April 7, 2020 7:36 PM
  • What version of .NET you're using?

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.producesattribute?view=aspnetcore-3.1


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles

    Tuesday, April 7, 2020 7:41 PM
    Moderator
  • Microsoft visual studio 2017 version 15.9.16

    .Net Framework 4.8.03752

    Tuesday, April 7, 2020 8:15 PM
  • Hi ,

      Thanks for your reply. I am getting the error for below lines.May be I am missing something. Can you clarify it please.

        [Produces("application/json")]
       
    [ApiController]

    An ASP.NET WebAPI controller is a ASP.NET MVC solution, just like an ASP.NET MVC UI, and both are using the ASP.NET MVC pipe-line.

    As a matter of fact,  you can have ASP.NET MVC UI controller  working with a view,  and you can have a WebAPI controller working with WebAPI in the same ASP.NET MVC project.

    This trutorial does just that of having view a MVC controller and WebAPI controller in the same ASP.MVC MVC project.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-1

    https://www.strathweb.com/2018/02/exploring-the-apicontrollerattribute-and-its-features-for-asp-net-core-mvc-2-1/

    [Produces("application/json")

    The WebAPI controller returns Json,  or it can return one of the other data exchange types. 

    [Produces("application/text")

    [Produces("application/XML")

    That HTTP 405  error code means in this situation that you tried to use a Get verb to access an Actionmethod that was defined for and is expecting a Post verb as an example resulting in 'method not allowed'.

    You have HTTP verb mismatch problems based on what the HTTP client is sending as the verb,  and what the HTTP Restful service ActionMethod is expecting as the verb.

    Tuesday, April 7, 2020 8:17 PM
  • I have created a new webapi project with core(version 2.1) and added the required verbs . But I am unable to test the verbs In postman or fiddle.

    In postman I am getting error that could not get any response.

    • The server couldn't send a response:
      Ensure that the backend is working properly
    • Self-signed SSL certificates are being blocked:
      Fix this by turning off 'SSL certificate verification' in Settings > General
    • Proxy configured incorrectly
      Ensure that proxy is configured correctly in Settings > Proxy
    • Request timeout:
      Change request timeout in Settings > General

    in fiddler I am getting 

    404 badrequest.

    https://localhost:44314/api/Emps/

    this is the json I am sending 

    {
        "lname": "Test",
        "fname": "Value",
        "sal": 2300
    }

    below is the method.

    // POST: api/Emps

     [HttpPost]
            public async Task<IActionResult> PostEmp([FromBody] Emp emp)
            {
                if (!ModelState.IsValid)
                {
                    return BadRequest(ModelState);
                }
                _context.Emp.Add(emp);
                await _context.SaveChangesAsync();
                return CreatedAtAction("GetEmp", new { id = emp.id }, emp);
            }

    Please guide me on this as I am new to webapi

    Thursday, April 16, 2020 7:38 AM
  • Please guide me on this as I am new to webapi

    1) Whatever you're being lead to use HTTPS stop doing iy and only use HTTP for now. You have no reason to be using HTTPS.

    2) The WebAPI template should have made a test controller that is doing 'GETS'. So you test using the test controller to make sure you are getting a good url that actually is using an action method of the WebAPI.

    3) Stop using Postman, use VS and its debugger to execute the WebAPI by configuring the project to use the browser where you will enter the url to the WebAPI action method through the browser address bar. At this time, you should just be doing some 'GETS'. You can make your own little Get method to see if you can access it.

    https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-enable-debugging-for-aspnet-applications?view=vs-2017

    Lastly ASP.NET WebAPI has a WebAPI forum in ASP.NET forums where you can get help..

    http://forums.asp.net/

     

    Thursday, April 16, 2020 3:12 PM