自定义 RouteConfig 会干扰 HttpPost 返回视图()



我配置了几个自定义路由,它似乎干扰了我的控制器。 GET 索引操作工作正常,但如果 POST 索引操作返回视图,则会出现混乱。 它最终会走向不同的路线或其他东西。 所以我 http://mywebsitehere/sms 去我的网站,它很好地运行了 GET Index 操作并呈现表单。

当我提交表单时,它会通过 POST 操作运行,如果 ModelState 无效,则进入return View(model)行。 然后我在NavigationController中出现错误,因为它试图获取一些不存在的路由数据。 它不应该在我的导航控制器中结束。

我的路线:

routes.MapRoute(
    name: "Notifications",
    url: "sms/{action}/{id}",
    defaults: new { controller = "SMS", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
    name: "Conference",
    url: "conf/{conferenceCode}/{controller}/{action}",
    defaults: new { conferenceCode = "", controller = "Home", action = "Overview" }
);

下面是我的控制器的示例:

public ActionResult Index()
{
    ViewBag.IsHome = true;
    ViewBag.Theme = "a";
    return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(SendSMSPageViewModel model)
{
    if (ModelState.IsValid)
    {
        // DO STUFF HERE
        ModelState.Clear();
        return RedirectToAction("Index");
    }
    return View(model);
}

最后我的观点:

@using (Html.BeginForm("Index", "SMS", FormMethod.Post, new { data_ajax = "false" }))
{
    @Html.AntiForgeryToken()
    @* My Form stuff here *@
}

这是我的一个错误。 有人给了我一些建议,我想出了问题所在。一些我以前没有发布的代码:

ViewBag.IsHome = false;

这是我的 GET 而不是我的 POST,没有它就会运行我不想运行的部分视图。

最新更新