1

I'm trying to delete a user from Default Membership of MVC but the passing parameters is always null. I've used [HttpDelete] attribute and [FromBody] but it gives "500 Server internal error". below is my code

    // Delete api/Del/user name

    public HttpResponseMessage DeleteUser(string user)
    {

        try
        {
            System.Web.Security.Membership.DeleteUser(user);
        }
        catch (Exception)
        {

            return Request.CreateResponse(HttpStatusCode.NotFound);
        }


        return Request.CreateResponse(HttpStatusCode.OK);
    }

This is my calling method with "Delete" verb.

http://localhost:3325/api/Del/haris2

I've created this webapi class for routing. I have a Get Method in Same controller with no arguments. Its working is fine.

WebApiConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace DatabaseService_WebAPI.App_Start
{
    public class WebApiConfig
    {
        public static void Configure(HttpConfiguration config)
        {
            // Filters
            config.Filters.Add(new QueryableAttribute());


            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
            config.Routes.MapHttpRoute(
               name: "DefaultApiwithAction",
               routeTemplate: "api/{controller}/{action}",
               defaults: new { id = RouteParameter.Optional }
           );

        }
    }
}
5
  • There may be an issue with your routing, can you provide your route configuration? Commented Nov 9, 2012 at 11:20
  • You definitely need the [HttpDelete] attribute at the top of your Delete method (not sure why adding that would be throwing a server error). Are you actually posting using DELETE as the verb? Commented Nov 9, 2012 at 11:31
  • Those 2 routes are essentially the same (ie, how will the routing mechanism determine if haris2 is an id or an action?) And I'm presuming the DeleteUser action is in the DelController controller? Commented Nov 9, 2012 at 11:37
  • now i used [HttpDelete] but the parameter is null. i`m using Rest Client Debugger for firefox. Commented Nov 9, 2012 at 11:38
  • yes, Delete user is a action in DelController. Now i comment my "DefaultApiwithAction" route. but the value is null :( Commented Nov 9, 2012 at 11:42

1 Answer 1

2

The problem is MVC maps your parameters by name. So there are two ways to fix your problem

  1. Change the name of your action parameter to id as that's what your mapped path expects e.g.

    public ActionResult DeleteUser(string id)
    {
        ...
    }
    
  2. Update your route to look for a user parameter instead of an id e.g.

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{user}",
        defaults: new { user = RouteParameter.Optional }
    );
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.