如何从控制器中重定向到 HTML 页面?

How do I redirect to HTML page from within a controller?

用户会收到一封包含 link 的电子邮件,他们必须单击该电子邮件才能验证其电子邮件地址。单击 link 后,用户应被重定向到两个静态 HTML 页面之一,一个显示“您已通过认证”,另一个显示“link 已过期”

我尝试了几种选择。首先,我向我的控制器添加了一个 Response.Redirect 和一个视图路径。我还尝试在我的 RouteConfig 文件中添加 routes.MapPageRoute 并更改我的重定向调用以尝试使用此名称,但这也不起作用。我查看了此示例以进行修复 ( Redirect to an html page inside Views Folder )

这是我尝试使用重定向访问 HTML 文件的代码:

EmailCertification.UpdateDBEmailCertified(userName, int.Parse(memberNumber), certSentDT);

return Redirect("~/Views/EmailCertification/EmailCertified.html");`

我得到的错误是:

Path to /Views/EmailEmailCertification/EmailCertified.html is not found. I verified the spelling and the path is all is correct.

如果我更改我的代码以在 RoutesConfig 中包含 MapPageRoute,它仍然不起作用。

这是我的路由配置:

routes.MapPageRoute("HtmlPage", "EmailCertifiedURL", "~/Views/EmailCertification/EmailCertied.html");`

这是我的控制器:

return Redirect("EmailCertifiedURL");

这是我的控制器,它是一个 HttpPost

public ActionResult EmailCertify(string userName, string memberNumber, string certSentDate)
        {
            DateTime certSentDT;

            long lngCertSent = long.Parse(certSentDate);

            certSentDT = new DateTime(lngCertSent);

            if (certSentDT < DateTime.Now.AddDays(-14))
                return Redirect("EmailOldURL");

            EmailCertification.UpdateDBEmailCertified(userName, int.Parse(memberNumber), certSentDT);
            return Redirect("~/Views/EmailCertification/EmailCertified.html");
        }

我得到的错误是

the controller doesn't have a action EmailCertifiedURL. This code I took from the above mentioned StackFlow article.

我只需要电子邮件 link 来触发控制器操作 EmailCertify 并将我重定向到静态 HTML 页面。

https://localhost:44344/EmailCertification/EmailCertify?userName=IS&memberNumber=3000050&certSentDate=636959314302036120 

我倾向于使用 RedirectToAction() 方法而不仅仅是 Redirect()

如果是不同的控制器,第二个参数需要是控制器的名称。

return RedirectToAction("EmailCertifiedURL", "EmailCertification");

这似乎很奇怪。解决方法是添加一个 returns 整个 html 没有布局的新操作。我的意思是,试试这个

public ActionResult CertifiedEmail(){ 
    return View(); 
} 

然后您应该为您的操作创建一个同名视图 (CertifiedEmail.cshtml),并在您的视图中粘贴所有 html。一开始你应该添加这段代码来删除布局

@{ 
    Layout = null; 
}
public ActionResult Questionnaire()
{
   return Redirect("~/MedicalHistory.html");
}