Haris
Haris

Reputation: 1209

Passing Parameters is null

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 }
           );

        }
    }
}

Upvotes: 1

Views: 1214

Answers (1)

James
James

Reputation: 82096

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 }
    );
    

Upvotes: 2

Related Questions