2017-03-09 36 views
0

你好,而在ASP.net MVC網站上工作時,我遇到了一個奇怪的問題。 雖然重定向到控制器中的另一個動作我想添加一個get值,我不喜歡這樣爲什麼ASP.NET MVC對視圖名稱使用GET操作參數值?

// 
    // POST: /Account/Register 
    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> Register(RegisterViewModel model) 
    { 
     if (ModelState.IsValid) 
     { 
      var user = new ApplicationUser { UserName = model.UserName, Email = model.Email }; 
      var result = await UserManager.CreateAsync(user, model.Password); 
      if (result.Succeeded) 
      { 
       //await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); 
       return RedirectToAction("ConfirmEmailAwait", "Account", new { userId = user.Id }); //<-----[LOOK HERE] 
      } 
      AddErrors(result); 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

    // 
    // GET: /Account/ConfirmEmailAwait?userId=d178b665-b616-4303-ae7d-00a663014109 
    [AllowAnonymous] 
    public async Task<ActionResult> ConfirmEmailAwait(string userId) 
    { 
     // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
     // Send an email with this link 
     string code = await UserManager.GenerateEmailConfirmationTokenAsync(userId); 
     var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = userId, code = code }, protocol: Request.Url.Scheme); 

     string emailHeader = "Confirm your account"; 
     string emailBody = "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"; 

     await UserManager.SendEmailAsync(userId, emailHeader, emailBody); 

     return View(userId); 
    } 

至於我可以看到這個代碼做什麼是應該做的,它會將用戶帶到正確的URL,因此它應該只是工作。但是,如果你看看這個形象在這裏:

Error image

構造的URL:http://localhost:55767/Account/ConfirmEmailAwait?userId=d178b665-b616-4303-ae7d-00a663014109

錯誤消息:視圖 'd178b665-b616-4303-ae7d-00a663014109' 或它的主人未找到或沒有視圖引擎支持搜索的位置

您會看到它正在搜索的視圖是我給它的獲取值,我覺得很奇怪。

有誰知道發生了什麼事?我只是犯了一個愚蠢的錯誤,沒有看到它?請幫助我,並提前感謝您。

+0

您不重定向到視圖。您重定向到操作。 – mason

+0

你需要顯示兩個控制器方法的代碼(不鏈接到它的圖像),所以可以添加一個答案 –

+0

@mason啊謝謝,我現在修復它 – TheRWS96

回答

1

您通過string作爲return View()的第一個參數,它使用this overload其中參數是視圖的名稱。您需要使用this overload作爲object來傳遞它,其中參數是您的模型。

return View((object)userId); 
1

您使用的是View(String viewName)超載,當你想View(Object model)

要使用String值作爲ViewModel你需要或者強制轉換爲Object第一...:

return this.View((Object)userId); 

...或者使用參數標籤:

return this.View(model: userId); 

使用參數標籤是我首選的方法,因爲轉換爲Object的原因可能不會立即對代碼的未來讀者顯而易見,但是您可能還想添加註釋,以便用戶知道爲什麼參數被明確命名以及爲什麼不應刪除,例如:

return this.View(model: userId); // Be careful not to call the `View(String viewName)` overload! 
0
// GET: /Account/ConfirmEmail 
[AllowAnonymous] 
public async Task<ActionResult> ConfirmEmailAwait(string userId) 
{ 
    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 
    // Send an email with this link 
    string code = await UserManager.GenerateEmailConfirmationTokenAsync(userId); 
    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = userId, code = code }, protocol: Request.Url.Scheme); 

    string emailHeader = "Confirm your account"; 
    string emailBody = "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"; 

    await UserManager.SendEmailAsync(userId, emailHeader, emailBody); 

    return View(userId); 
} 

在上面的代碼中,注意 「返回視圖(用戶id);」

「userId」是一個字符串 - 當返回View(string)時,Action認爲你的userId實際上是View(.cshtml文件)的路徑。

這就是爲什麼代碼出現異常,說它無法找到與提供的路徑匹配的視圖。

我希望有道理:-)

相關問題