我在家庭控制器中设置正确的路线时遇到问题。 我试图为我的电子邮件设置路由输入字段。这是我的cshtml代码片段:
<form method="post" action="/home/emailsignup">
<input type="text" class="form-control" name="email" placeholder="you@example.com">
<span class="input-group-btn btnstateless">
<input type="submit" id="mailSignup"value="SUBSCRIBE"/>
</span>
</div>
</form>
我试图在HomeController.cs中设置行动路线 这是我的代码的一部分:
[Route("home/emailsignup")]
[HttpGet]
public ActionResult EmailSignupGet()
{
var cVM = new ConfirmedViewModel
{
confirmationTitle = "Thanks!",
message = "",
nextStep = "<a href=\"/\">Return home</a>"
};
return View("Confirmed", cVM);
}
[HttpPost]
public ActionResult EmailSignup(string email)
{
if (UtilsClass.IsValidEmail(email))
{
Email.GreenArrowHelper.AddOrUpdateUserByEmailFetchAllInfo(email).ConfigureAwait(false);
var cVM = new ConfirmedViewModel();
cVM.confirmationTitle = "Thanks!";
cVM.message = "";
cVM.nextStep = "<a href=\"/\">Return home</a>";
return View("Confirmed", cVM);
}
else
{
ErrorViewModel eVM = new ErrorViewModel
{
title = "Hmm, something's not right.",
message = "That doesn't look like a valid email address. Please try again.",
nextstep = "<a href=\"/\">Return home</a>"
};
return View("Error", eVM);
}
}
我试图返回我的消息,但是,我点击了订阅按钮获得了HTTP 404.我做错了什么?
答案 0 :(得分:1)
您错过了帖子操作的路线
//POST home/emailsignup
[Route("home/emailsignup")]
[HttpPost]
public ActionResult EmailSignup(string email) {
//...
}
在控制器上使用属性路由时,您需要将其应用于所需的操作,否则会在您遇到的情况下找不到404。