网址自动更改

时间:2014-11-06 03:00:23

标签: asp.net-mvc url redirect

这是我的路线:

routes.MapRoute(
"LastTwoRoute",
"thong-ke-ket-qua-xo-so-2-so-cuoi/{cityID}/{numbers}/{days}/{onlySpecial}",
new { controller = "LastTwo", action = "Index", 
        cityID = "MB", 
        numbers = "",
        days = 1000,
        onlySpecial = false
});

这是控制器:

[HttpGet]
public ActionResult Index(string cityID, string numbers, int days, bool onlySpecial)
{

    LastTwoParameters lastTwoParameters = new LastTwoParameters();
    lastTwoParameters.listCities = Common.GetDropDownCitiesList();
    lastTwoParameters.listLastTwoResult = new List<getReport_LastTwo_Result>();

    if (TempData["Redirection"] != null || !string.IsNullOrEmpty(numbers) )
    {
        if (!string.IsNullOrEmpty(cityID) && days > 0)
        {
            using (KQXS context = new KQXS())
            {
                lastTwoParameters.listLastTwoResult = context.getReport_LastTwo(cityID, numbers, days, onlySpecial).ToList();
            }
        }

    }
    return View(lastTwoParameters);
}

[HttpPost]//Run action method on form submission
public ActionResult Index(List<Cities> c, string cityID, string numbers, int days, bool onlySpecial)
{
    TempData["Redirection"] = true; 
    return RedirectToAction("Index", new { cityID = cityID, numbers = numbers, days = days, onlySpecial = onlySpecial });
}

当我第一次访问控制器时,网址是:

http://example.com/thong-ke-ket-qua-xo-so-2-so-cuoi

当我从参数面板中选择任意参数并单击提交时:

http://example.com/thong-ke-ket-qua-xo-so-2-so-cuoi/MB/5/70/True

但由于某些原因,有时网址在提交后会变为:

http://example.com/LastTwo?cityID=NBD&numbers=&days=70&onlySpecial=False

thong-ke-ket-qua-xo-so-2-so-cuoi部分已替换为控制器名称:LastTwo,参数部分变为?para=value

为什么会发生这种情况?如何防止这种情况发生?谢谢!

1 个答案:

答案 0 :(得分:1)

您需要使用RedirectToRoute而不是在发布操作中使用RedirectToAction来保持URL状态。如下所示

return RedirectToRoute("LastTwoRoute",new { cityID = cityID, numbers = numbers, days = days, onlySpecial = onlySpecial });

有关详细信息,请查看此链接 http://haacked.com/archive/2011/02/02/redirecting-routes-to-maintain-persistent-urls.aspx/