2013-02-28 81 views
0

昨天我有這個工作,爲什麼它停止工作是完全超越了我。我有以下基本場景:MVC 4路由/控制器邏輯 - 基本出錯

在我的應用程序Tenant和Landlord中有兩種類型的角色。當租戶登錄時,他們應該被引導到租戶個人資料頁面,房東也是如此(目前我只與租戶合作)。

這裏的場景的工作流程:

  1. 用戶通過_LoginPartial登錄,我敢肯定,在部分的邏輯是正確的,但由於某種原因SO不會讓我propertly粘貼剃鬚刀代碼在這裏...

  2. 單擊登錄按鈕調用AccountController中的LoginActionResult。該方法驗證用戶並檢查他們在哪個角色中。如果用戶在角色租戶中,則他們應該被重定向到的TenantsController中的MyProfileActionResult。代碼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); 
    } 
    
  3. 在被調用MyProfile應顯示用戶的個人資料。代碼MyProfileActionResult

    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的視圖。我的路由/控制器邏輯有問題嗎?幫助將不勝感激。

+0

你是否已經將代碼放到這一行?...... return RedirectToAction(「MyProfile」,「Tenants」); – 2013-02-28 13:01:47

+0

當我調試時,'if'不滿意,因爲某些原因,角色似乎不在那裏 - 他們是昨天。 – MattSull 2013-02-28 13:25:52

+0

是的,這就是爲什麼我提供你的代碼在我的答案,以測試它是否令人滿意,如果沒有:) 謝謝 – 2013-02-28 13:27:06

回答

0

問題正在通過這引起了...

if (Roles.IsUserInRole("Tenant")) 

...而不是這樣的:

if (Roles.IsUserInRole(model.UserName, "Tenant")) 

前者可在用戶登錄後調用。似乎後者必須在Login內調用時使用 - 可能會有一些事情與用戶不在會議/完全登錄等。

它無論如何工作。

0

您的Login行動AccountsController返回View(model)

按照慣例,MVC將查找與匹配控制器名稱的文件夾中的操作名稱匹配的視圖名稱。因此它正在尋找帳戶控制器下的登錄視圖。

+0

謝謝,我明白爲什麼我得到那個錯誤。你能看到我的邏輯有什麼問題 - 因爲我知道登錄憑證是正確的嗎? – MattSull 2013-02-28 13:22:13

+0

既然你有一個_LoginPartial,你能返回那個帶有模型錯誤的局部視圖嗎?登錄憑據是正確的是一個運行時行爲,你看到的錯誤將在編譯時出現。此外,如果您的登錄憑據錯誤,您仍然需要處理它。 – 2013-02-28 13:26:45

2

由於這些線被執行:

// If we got this far, something failed, redisplay form 
ModelState.AddModelError("", "The user name or password provided is incorrect."); 
return View(model); 

顯然任一登錄嘗試失敗,或者輸入無效。現在執行Login操作的最後一次返回。由於您尚未直接聲明視圖的名稱,因此命名約定會嘗試查找與該操作具有相同名稱的視圖(在此情況下爲Login)。明確地嘗試指定視圖名稱:

return View("ViewName", model); 
+0

登錄憑證絕對有效。我現在明白了爲什麼我得到這個錯誤,謝謝。你能否在錯誤發生之前看到我想要做的事情 - 儘管我知道登錄憑證是正確的? – MattSull 2013-02-28 13:19:54

+0

@ MattSull87,角色可能會有變化嗎?如果由於某些原因(如登錄用戶不是租戶)而不滿足'if'條件,重定向也不會發生。 – Andrei 2013-02-28 13:21:36

+0

我在想它一定是這樣 - 我調試過了,當它到達'如果'我看不到我定義的角色......我會更多地考慮它,謝謝。 – MattSull 2013-02-28 13:24:44

0

執行下面的技巧,並檢查是否打印「你好用戶」在瀏覽器上,如果是的話,那就意味着它不驗證用戶和重定向如提及到登錄頁面你的方法的底部。

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 Content("Hello User"); 
} 
0

打開你的web.config。

看看這裏:

<system.web> 
    <compilation debug="true" targetFramework="4.5" /> 
    <httpRuntime targetFramework="4.5" /> 
    <authentication mode="Forms"> 
     <forms loginUrl="~/Account/Login" timeout="2880" /> 
    </authentication> 
+0

你能給我更多的信息,究竟這將實現什麼? – MattSull 2013-02-28 13:21:20

+0

對不起,應該多說明一下。查看你的web.config,並確保它沒有指向該位置的View。我曾經遇到過這種情況,這就是問題所在。這只是確認一件事是指向正確的地方。或者我誤解了這個問題。 – 2013-02-28 13:25:07