Stu*_*art 13 c# asp.net-web-api
我有一个控制器.
public sealed class AccountsController : BaseApiController
{
private readonly IDatabaseAdapter _databaseAdapter;
public AccountsController(IDatabaseAdapter databaseAdapter)
{
_databaseAdapter = databaseAdapter;
}
[AllowAnonymous]
[Route("create")]
public async Task<IHttpActionResult> CreateUser(CreateUserBindingModel createUserModel)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
if (! await _databaseAdapter.DoesAgentExist(createUserModel.UserName))
return BadRequest();
if (await _databaseAdapter.DoesAgentHaveAccount(createUserModel.UserName))
return BadRequest();
// Create account.
var password = PasswordHelper.GeneratePassword(32);
createUserModel.Password = password;
createUserModel.ConfirmPassword = password;
var user = new ApplicationUser
{
UserName = createUserModel.UserName,
};
var addUserResult = await AppUserManager.CreateAsync(user, createUserModel.Password);
if (!addUserResult.Succeeded)
return GetErrorResult(addUserResult);
var locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));
return Created(locationHeader, ModelFactory.Create(user));
}
}
Run Code Online (Sandbox Code Playgroud)
当我将以下提琴手发送到create方法时.
http:// localhost:59430/api/accounts/create User-Agent:Fiddler
Content-Type:application/json接受:application/json主机:localhost:59430内容长度:106
{"UserName":"a.xxxxx","密码":"xxxxxx","ConfirmPassword":"xxxxxx",}
它归结为以下行:
var addUserResult = await AppUserManager.CreateAsync(user, createUserModel.Password);
然后发生以下异常
{"message":"发生错误.","exceptionMessage":"找不到方法:'System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get_Request()'.","exceptionType":" System.MissingMethodException","stackTrace":"在WebAuth.Controllers.BaseApiController.get_AppUserManager()\ r \n,位于C:\ Users\stuarts\Documents\Visual Studio 2017中的WebAuth.Controllers.AccountsController.d__3.MoveNext() Projects\WebAuth\WebAuth\Controllers\AccountsController.cs:第76行\ r \n ---从抛出异常的上一个位置开始的堆栈跟踪结束---\r \n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \n在System.Threading.Tasks.TaskHelpersExtensions.d__3`1.MoveNext()\ r \n ---结束从抛出异常的先前位置堆栈跟踪---\r \n在System.Runtime.CompilerServices上的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \n System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\ r \n中的.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \n \n ---从抛出异常的上一个位置开始的堆栈跟踪结束--- r \n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \n,位于System.Web.Http.Controllers.ActionFilterResult的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \n. d__2.MoveNext()\ r \n ---从抛出异常的上一个位置开始的堆栈跟踪结束---\r \n在系统.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \n在系统中System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"}中的.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \n
有谁知道发生了什么?我不知道为什么它找不到那种方法.
我的bin文件夹包含
System.Web.Http.dll System.Web.Http.Owin.dll System.Net.Http.dll
ApplicationUserManager
public sealed class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store) : base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options,
IOwinContext context)
{
var appDbContext = context.Get<ApplicationDbContext>();
var appUserManager = new ApplicationUserManager(new UserStore<ApplicationUser>(appDbContext));
appUserManager.UserValidator = new UserValidator<ApplicationUser>(appUserManager)
{
AllowOnlyAlphanumericUserNames = true,
RequireUniqueEmail = false,
};
appUserManager.PasswordValidator = new PasswordValidator
{
RequiredLength = 12,
RequireNonLetterOrDigit = true,
RequireUppercase = true,
RequireLowercase = true,
RequireDigit = true
};
appUserManager.MaxFailedAccessAttemptsBeforeLockout = 3;
appUserManager.DefaultAccountLockoutTimeSpan = TimeSpan.FromHours(1);
return appUserManager;
}
}
Run Code Online (Sandbox Code Playgroud)
BaseApiController
public class BaseApiController : ApiController
{
private ModelFactory _modelFactory;
private readonly ApplicationUserManager _applicationUserManager = null;
protected ApplicationUserManager AppUserManager => _applicationUserManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
protected ModelFactory ModelFactory => _modelFactory ?? (_modelFactory = new ModelFactory(Request, AppUserManager));
protected IHttpActionResult GetErrorResult(IdentityResult result)
{
if (result == null)
return InternalServerError();
if (result.Succeeded) return null;
if (result.Errors != null)
foreach (var error in result.Errors)
ModelState.AddModelError(string.Empty, error);
if (ModelState.IsValid)
return BadRequest();
return BadRequest(ModelState);
}
private readonly ApplicationRoleManager _appRoleManager = null;
protected ApplicationRoleManager AppRoleManager => _appRoleManager ?? Request.GetOwinContext().GetUserManager<ApplicationRoleManager>();
}
Run Code Online (Sandbox Code Playgroud)
Stu*_*art 48
我找到了解决方案.
在我构建时,有一些警告会进入输出窗口,但不会显示在主错误/警告窗口中.
他们与汇编冲突有关,并建议将汇编重定向放在web.Config中.
一旦我完成了所有(大约80)它现在有效.
例如
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
</dependentAssembly>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7828 次 |
| 最近记录: |