richard88
richard88

Reputation: 1

Api Post error:The requested resource does not support http method 'GET'.88

I'm new to asp.net, and I'm trying to create an API to add a field to my database but I'm getting this message: The requested resource does not support http method 'GET'. I really need help. Here is my code:

public class PostNewUserController : ApiController
{
    RegisterViewModel newUser=new RegisterViewModel() ;


    public HttpResponseMessage PostUser(string userName, string password, string confirmPassword)
    {
        UsersAdminController us = new UsersAdminController();
        newUser.Email = userName;
        newUser.Password = password;
        newUser.ConfirmPassword = confirmPassword;

        var user = new ApplicationUser { UserName = newUser.Email, Email = newUser.Email };
        var adminresult = us.UserManager.CreateAsync(user, newUser.Password);

        var response = Request.CreateResponse<RegisterViewModel>(HttpStatusCode.Created, newUser);
        return response;
    }
}

And this is the routeConfig code

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

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

Thanks in advance

Upvotes: 0

Views: 1445

Answers (2)

neo
neo

Reputation: 2042

The requested resource does not support http method 'GET'

The message means that you are making a get request and there is no method defined in your controller that starts with word

Get

When you make your http request, you have to make "POST" request not "GET" request because your controller only has a POST action.

Upvotes: 0

Charlie
Charlie

Reputation: 177

you should prefix your action like this server side :

[HttpPost()]
[Route()]
public HttpResponseMessage PostUser(string userName, string password, string confirmPassword)

Edit: also you should create a model containing the data of your request instead of passing each elements, this will be easier when you will need to provide 50 elements in your action but this is another matter

Upvotes: 1

Related Questions