【发布时间】:2015-03-27 09:59:31
【问题描述】:
所以我正在尝试使用我现有的 Web API 创建一个注册页面。 AccountController 上有一些 post 方法,但除了 2 之外,我已将它们全部注释掉。这是它们的样子:
/// <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();
}
}
我的角度控制器看起来像这样:
.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!');
});
};
}]);
如果我在 AccountController 中注释掉 Logout 功能,然后尝试注册,它会正常注册,并且所有字段都已填充且正确。 如果我取消注释 Logout 并发送相同的表单,我会收到错误:
找到多个与请求匹配的操作:
在 AccountController 类型上注册
在 AccountController 类型上注销
如您所见,Logout 是无参数的,所以我不确定为什么会收到此错误。谁能告诉我我做错了什么?
【问题讨论】:
-
看起来不错。在您的应用程序启动中,您是否通过以下方式配置了 web api: GlobalConfiguration.Configure(WebApiConfig.Register)?
-
如果我注释掉 Logout 功能,它可以完美运行.....
标签: asp.net angularjs asp.net-web-api