2015-03-18 63 views
0

我有一個asp,我有單選按鈕爲用戶選擇一個角色。在aspx中,我想更改用戶刪除舊用戶的角色並添加新用戶,這很簡單。更改用戶角色

問題是,我得到的ApplicationUserManager具有所有的異步方法。異步和asp頁面相處得不好給出錯誤:

此時無法啓動異步操作。異步操作只能在異步處理程序或模塊內啓動,或者在頁生命週期中的某些事件期間啓動。如果執行頁面時發生此異常,請確保頁面標記爲<%@ Page Async =「true」%>。此異常也可能表示嘗試調用「異步無效」方法,該方法在ASP.NET請求處理中通常不受支持。相反,異步方法應該返回一個Task,並且調用者應該等待它。

對於我給出這個錯誤的異步嘗試,我稱之爲異步任務。

protected async void GrantPrivlege_Click(object sender, EventArgs e) 
    { 
     ApplicationUserManager manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
     string groupId = AspHelper.StringGuid(Request.Url.Segments[3]); 
     string userId = Request.Url.Segments[4]; 
     IList<string> roles = await WaitGetRoles(userId); 
     foreach (string roleStr in roles) { 
      await WaitRemoveFromRole(userId, roleStr); 
     } 
     ApplicationUser.RoleEnum role = (ApplicationUser.RoleEnum)Enum.Parse(typeof(ApplicationUser.RoleEnum), PrivilegeList.SelectedValue, true); 
     await WaitAddToRole(userId, role.ToString());  
     Response.Redirect("/Admin/GroupAdmin/" + groupId); 
    } 

    protected async Task<IList<string>> WaitGetRoles(string userId) 
    { 
     ApplicationUserManager manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
     IList<string> roles = await manager.GetRolesAsync(userId); 
     return roles; 
    } 

    protected async Task<bool> WaitRemoveFromRole(string userId, string roleStr) 
    { 
     ApplicationUserManager manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
     await manager.RemoveFromRoleAsync(userId, roleStr); 
     return true; 
    } 

    protected async Task<bool> WaitAddToRole(string userId, string roleStr) 
    { 
     ApplicationUserManager manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
     await manager.AddToRoleAsync(userId, roleStr); 
     return true;  
    } 

真正的拼圖形式是,Visual Studio項目附帶的Admin Register頁面具有AddRole方法沒有異步調用。

無論是註冊頁面,我的網頁是與創建

ApplicationUserManager manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); 

的管理和都做一個的onclick方法的aspx.cs一個aspx。我真的希望我得到註冊ApplicationUserManager使用UserManagerExtensions具有AddToRoles而不是管理與Microsoft.AspNet.Identity與所有異步。不同的是可能正在登錄,而註冊ASP不是,登錄後。

是否有人請告訴我如何獲得UserManagerExtensions版本的ApplicationUserManager,或者如果這是不可能的如何可靠地在aspx中獲得異步工作。

我被封鎖,因爲這一點,我會繼續谷歌搜索解決方案。

回答

0

有趣的是,張貼這個問題給了我一些新的谷歌條款嘗試,我發現http://www.hanselman.com/blog/TheMagicOfUsingAsynchronousMethodsInASPNET45PlusAnImportantGotcha.aspx它展示瞭如何在ASP.Net中執行異步aspx。

把異步是真的頁面標記是很酷的,因爲然後Visual Studio添加一個異步標記給我的所有電話,神奇的是,我的等待異步調用工作正常。

// I added Async="true" to my aspx page 
 
<%@ Page Async="true" Title="Async" Language="C#" CodeBehind="Async.aspx.cs" Inherits="Whatever" %> 
 

 
// and then in my aspx.cs file await GetRolesAsync() works now 
 
    public async Task LoadPage() 
 
    { 
 
     string userId = Request.Url.Segments[4]; 
 
     ApplicationUserManager manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
 
     IList<string> roles = await manager.GetRolesAsync(userId); 
 
     if (roles.Count > 0) { 
 
      PrivilegeList.SelectedValue = roles[0]; 
 
     }     
 
    }

運行與強制異步調用的頁面從ApplicationUserManager現在工作得很好。

相關問題