system.web.http.apicontroller.get_request()中缺少方法



我有一个控制器。

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

当我将以下提琴手发送到创建方法时。

content-type:application/json accept:application/json主机: 本地主机:59430内容长度:106

{"用户名":" a.xxxxx","密码":" xxxxxx", " confirm vassword":" xxxxxx",}

它到达以下行:

var addUserResult = await AppUserManager.CreateAsync(user, createUserModel.Password);

然后发生以下异常

{"消息":"发生了错误。"," exceptionMessage":"方法 找不到:'system.net.http.httprequestmessage system.web.http.apicontroller.get_request()'。 " system.missingmethodexception"," stacktrace": webauth.controllers.baseapicontroller.get_appusermanager() r n at webauth.controllers.accountscontroller.d__3.movenext()in C: Users Stuarts Documents Visual Studio 2017 projects webauth webauth controlters accountscontroller.cs:line 76 r n ---堆栈跟踪的结束来自以前的位置 被扔了--- r n system.runtime.compilerservices.taskawaiter.throwfornonsuccess(任务 任务) r n at system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(任务 任务) r n at system.threading.tasks.taskhelpersextensions.d__3`1.movenext() r n ------ 堆栈跟踪的结尾从抛出例外的位置 --- r n在system.runtime.com.pilerservices.taskawaiter.throwfornonsuccess(任务) 任务) r n at system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(任务 任务) r n at system.web.http.controllers.apicontrollerareactionInvoker.d__0.movenext() r n ---------- 堆栈跟踪的结尾从抛出例外的位置 --- r n在system.runtime.com.pilerservices.taskawaiter.throwfornonsuccess(任务) 任务) r n at system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(任务 任务) r n at system.web.http.controllers.actionfilterresult.d__2.movenext() r n -------- 堆栈跟踪的结尾从抛出例外的位置 --- r n在system.runtime.com.pilerservices.taskawaiter.throwfornonsuccess(任务) 任务) r n at system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(任务 任务) r n at system.web.http.dispatcher.httpcontrollerdispatcher.d__1.movenext() }

有人知道发生了什么事吗?我不知道为什么找不到该方法。

我的bin文件夹包含

system.web.http.dllsystem.web.http.owin.dllsystem.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;
        }
    }

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

我找到了一个解决方案。

当我构建时,有构建警告将进入输出窗口,但没有在主错误/警告窗口中显示。

他们与集会冲突有关,并建议将大会重定向放在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>

最新更新