1

So I am trying to create a register page with my existing web API. There are a few post methods on the AccountController, but I have commented them all out apart from 2. Here is what they look like:

/// <summary>
/// Handles all account related functions.
/// </summary>
[Authorize]
[RoutePrefix("Account")]
public class AccountController : ApiController
{
    private const string LocalLoginProvider = "Local";

    private readonly IUnitOfWork unitOfWork;
    private readonly UserService<User> service;
    private readonly UserLoginService userLoginService;
    private readonly RoleService<User> roleService;
    private readonly ISecureDataFormat<AuthenticationTicket> accessTokenFormat;

    /// <summary>
    /// Parameterless Constructor which references the startup config auth options access token format.
    /// </summary>
    public AccountController()
        : this(StartupConfig.OAuthOptions.AccessTokenFormat)
    {
    }

    /// <summary>
    /// Constructor with the access token format parameter.
    /// </summary>
    /// <param name="accessTokenFormat">The parameter for specifying the access token format.</param>
    public AccountController(ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
    {
        this.unitOfWork = new UnitOfWork<DatabaseContext>();
        this.service = new UserService<User>(this.unitOfWork, false, true);
        this.userLoginService = new UserLoginService(this.unitOfWork);
        this.roleService = new RoleService<User>(this.unitOfWork);
        this.accessTokenFormat = accessTokenFormat;
    }

    // POST api/account/register
    /// <summary>
    /// Registers a new user with the system.
    /// </summary>
    /// <param name="model">The model representing the user.</param>
    /// <returns></returns>
    [HttpPost]
    [AllowAnonymous]
    [Route("Register")]
    [ResponseType(typeof(r3plica.Identity.IdentityResult))]
    public async Task<IHttpActionResult> Register(RegisterBindingViewModel model)
    {
        if (!ModelState.IsValid)
            return BadRequest(ModelState);

        var user = new User
        {
            UserName = model.Email,
            Email = model.Email
        };

        var result = await service.CreateAsync(user, model.Password);
        var errorResult = GetErrorResult(result);

        if (errorResult != null)
            return errorResult;

        await this.unitOfWork.SaveChangesAsync();

        return Ok(user.Id);
    }

    // POST api/account/logout
    /// <summary>
    /// Logs the current user out.
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [Route("Logout")]
    public IHttpActionResult Logout()
    {
        Authentication.SignOut(CookieAuthenticationDefaults.AuthenticationType);

        return Ok();
    }
}

and my angular controller looks like this:

.controller('RegisterController', ['$state', 'Api', function ($state, api) {
    var self = this;

    self.model = {
        email: '',
        password: '',
        confirmPassword: ''
    };

    self.register = function () {
        api.post('account/register', self.model).then(function (response) {
            console.log(response);
        }, function (error) {
            alert('error!');
        });
    };
}]);

If I comment out the Logout function in my AccountController, then try to register, it registers fine and all the fields are populated and correct. If I uncomment Logout and send the same form, I get an error:

Multiple actions were found that match the request:

Register on type AccountController

Logout on type AccountController

As you can see Logout is parameterless, so I am not sure why I am getting this error. Can someone tell me what I am doing wrong?

2
  • 1
    It looks fine. In your application start, have you configured web api in the following way: GlobalConfiguration.Configure(WebApiConfig.Register)? Commented Mar 27, 2015 at 10:31
  • If I comment out the Logout function it works perfectly..... Commented Mar 27, 2015 at 10:47

1 Answer 1

1

It turns out that there is a very important line of code that is required in your WebApiConfig

config.MapHttpAttributeRoutes();

Without this, having a route set up would be ignored, so saying that this bit of code:

[Route("Register")] 

would not be hit. Because both Register and Logout were decorated with [HttpPost], it meant that they were being treated as the same.

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.