C# 为什么ASP.NET MVC使用GET action参数值作为视图名称?

C# 为什么ASP.NET MVC使用GET action参数值作为视图名称?,c#,asp.net,asp.net-mvc,get,C#,Asp.net,Asp.net Mvc,Get,你好,在ASP.NETMVC网站上工作时,我遇到了一个奇怪的问题。 当重定向到控制器中的另一个操作时,我想添加一个get值,我就是这样做的 // // POST: /Account/Register [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Register(RegisterViewModel model)

你好,在ASP.NETMVC网站上工作时,我遇到了一个奇怪的问题。 当重定向到控制器中的另一个操作时,我想添加一个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);
    }
//
//职位:/Account/Register
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务”;
等待UserManager.sendmailasync(userId、emailHeader、emailBody);
返回视图(userId);
}
据我所知,这段代码执行了它应该执行的操作,它将用户发送到正确的URL,因此它应该可以正常工作。但是如果您看下面的图片:

构造的URL:

错误消息:未找到视图“d178b665-b616-4303-ae7d-00A66301419”或其主视图,或者没有视图引擎支持搜索的位置

您将看到它正在搜索的视图是我给它的get值,我发现它非常奇怪


有人知道发生了什么吗?我只是犯了一个愚蠢的错误而没有看到它吗?请帮助我,并提前向您表示感谢。

您将
字符串作为
返回视图()的第一个参数传递
使用,其中参数是视图的名称。您需要将其作为
对象传递
使用,其中参数是您的模型

return View((object)userId);

字符串
作为
返回视图()
的第一个参数进行传递,其中参数是视图的名称。您需要将其作为
对象进行传递,其中参数是您的模型

return View((object)userId);

当您需要
视图(对象模型)
时,您正在使用
视图(字符串视图名称)
重载

要将
字符串
值用作
视图模型
,您需要先将其转换为
对象

return this.View( (Object)userId );
…或使用参数标签:

return this.View( model: userId );
使用参数标签是我的首选方法,因为强制转换为对象的原因可能不会立即为代码的未来读者所了解,但您可能也希望添加注释,以便用户知道参数的显式命名原因以及不应删除的原因,例如:

return this.View( model: userId ); // Be careful not to call the `View(String viewName)` overload!

当您需要
视图(对象模型)
时,您正在使用
视图(字符串视图名称)
重载

要将
字符串
值用作
视图模型
,您需要先将其转换为
对象

return this.View( (Object)userId );
…或使用参数标签:

return this.View( model: userId );
使用参数标签是我的首选方法,因为强制转换为对象的原因可能不会立即为代码的未来读者所了解,但您可能也希望添加注释,以便用户知道参数的显式命名原因以及不应删除的原因,例如:

return this.View( model: userId ); // Be careful not to call the `View(String viewName)` overload!
//获取:/Account/ConfirmEmail
[异名]
公共异步任务”;
等待UserManager.sendmailasync(userId、emailHeader、emailBody);
返回视图(userId);
}
在上面的代码中,注意“returnview(userId);”

“userId”是一个字符串-当返回视图(string)时,操作认为您的userId实际上是视图的路径(cshtml文件)

这就是代码出现异常的原因,表示无法找到与提供的路径匹配的视图

我希望这是有意义的:-)

//GET:/Account/ConfirmEmail
[异名]
公共异步任务”;
等待UserManager.sendmailasync(userId、emailHeader、emailBody);
返回视图(userId);
}
在上面的代码中,注意“返回视图(userId)”;"

“userId”是一个字符串-当返回视图(string)时,操作认为您的userId实际上是视图的路径(cshtml文件)

这就是代码出现异常的原因,表示无法找到与提供的路径匹配的视图


我希望这是有意义的:-)

您不需要重定向到视图。您需要重定向到操作。您需要显示两种控制器方法的代码(而不是指向其图像的链接)因此,答案是肯定的added@mason啊,谢谢,我修好了now@StephenMuecke我更新了问题,您不重定向到视图。您重定向到操作。您需要显示两种控制器方法的代码(而不是指向其图像的链接),以便可以找到答案added@mason啊,谢谢,我修好了now@StephenMuecke我更新了问题