[英]Why is ASP.NET MVC using the GET action parameter value for the View name?
您好,在ASP.net MVC網站上工作時,遇到一個奇怪的問題。 當我重定向到Controller中的另一個Action時,我想添加一個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,因此它應該可以正常工作。 但是,如果您在此處查看此圖片:
構造的URL: http:// localhost:55767 / Account / ConfirmEmailAwait?userId = d178b665-b616-4303-ae7d-00a663014109
錯誤消息:找不到視圖'd178b665-b616-4303-ae7d-00a663014109'或其主視圖,或者沒有視圖引擎支持搜索到的位置
您將看到它正在搜索的視圖是我賦予它的獲取值,我覺得這很奇怪。
有人知道發生了什么嗎? 我只是在犯一個愚蠢的錯誤而沒有看到它嗎? 請幫幫我,謝謝你。
當您需要View(Object model)
時,您正在使用View(String viewName)
重載。
要將String
值用作ViewModel
您需要先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!
// 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);
}
在上面的代碼中,請注意“ return View(userId);”。
“ userId”是一個字符串-返回View(string)時,Action會認為您的userId實際上是View的路徑(.cshtml文件)。
這就是為什么代碼帶有異常的原因,即無法找到與提供的路徑匹配的視圖。
我希望這是有道理的 :-)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.