昨天我有這個工作,爲什麼它停止工作是完全超越了我。我有以下基本場景:MVC 4路由/控制器邏輯 - 基本出錯
在我的應用程序Tenant和Landlord中有兩種類型的角色。當租戶登錄時,他們應該被引導到租戶個人資料頁面,房東也是如此(目前我只與租戶合作)。
這裏的場景的工作流程:
用戶通過
_LoginPartial
登錄,我敢肯定,在部分的邏輯是正確的,但由於某種原因SO不會讓我propertly粘貼剃鬚刀代碼在這裏...單擊登錄按鈕調用
AccountController
中的Login
ActionResult
。該方法驗證用戶並檢查他們在哪個角色中。如果用戶在角色租戶中,則他們應該被重定向到的TenantsController
中的MyProfile
ActionResult
。代碼Login
:public ActionResult Login(LoginModel model) { if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe)) { if (Roles.IsUserInRole("Tenant")) { return RedirectToAction("MyProfile", "Tenants"); } } // If we got this far, something failed, redisplay form ModelState.AddModelError("", "The user name or password provided is incorrect."); return View(model); }
在被調用
MyProfile
應顯示用戶的個人資料。代碼MyProfile
ActionResult
:public ActionResult MyProfile() { var db = new LetLordContext(); var currentTenant = db.UserProfile.First(t => t.UserName == HttpContext.User.Identity.Name); return View(currentTenant); }
正如我說,這是工作在昨天,但是當我點擊_RegisterPartial
登錄按鈕我收到以下錯誤:
視圖「登錄」或其主人沒有被發現或者沒有視圖引擎支持搜索到的位置。搜索以下位置: 〜/ Views/Account/Login.aspx 〜/ Views/Account/Login.ascx 〜/ Views/Shared/Login.aspx 〜/ Views/Shared/Login.ascx 〜/ Views /Account/Login.cshtml 〜/查看/帳號/ Login.vbhtml 〜/查看/共享/ Login.cshtml 〜/查看/共享/ Login.vbhtml
爲什麼MVC尋找一個叫做視圖登錄?我沒有指定它路由到一個名爲Login的視圖。我的路由/控制器邏輯有問題嗎?幫助將不勝感激。
你是否已經將代碼放到這一行?...... return RedirectToAction(「MyProfile」,「Tenants」); – 2013-02-28 13:01:47
當我調試時,'if'不滿意,因爲某些原因,角色似乎不在那裏 - 他們是昨天。 – MattSull 2013-02-28 13:25:52
是的,這就是爲什麼我提供你的代碼在我的答案,以測試它是否令人滿意,如果沒有:) 謝謝 – 2013-02-28 13:27:06