2

我不确定我做了什么,但现在我的网站将其显示为 url:

http://localhost:53187/Customer/AccountScreen?UserName=testx&Password=test12345&RememberMe=False&AccountId=5b89d595-ef19-4feb-b95d-bf39672c9ac4

我正在调用客户的帐户屏幕,如下所示:

return RedirectToAction("AccountScreen", "Customer", model);

所以,我不明白为什么它现在在 url 中显示这个。这对我来说真的是一个不好的做法。

有没有办法防止这种情况?

4

3 回答 3

2

您可以只包含您感兴趣的值:

return RedirectToAction(
    "AccountScreen", 
    "Customer", 
    new { AccountId = mode.AccountId }
);

将重定向到http://localhost:53187/Customer/AccountScreen?AccountId=5b89d595-ef19-4feb-b95d-bf39672c9ac4

于 2012-12-29T22:51:04.573 回答
2

目前尚不清楚为什么要将模型传递给 RedirectToAction 方法。第三个参数用于 routeValues。

无论您传递给 routeValues 参数,都会在 url 中公开其属性。去掉第三个参数就OK了。如果您需要将任何内容传递给 AccountScreen,请使用类似

return RedirectToAction("AccountScreen", "Customer", new { id = model.Id });
于 2012-12-30T00:33:12.317 回答
1

RedirectToAction方法向浏览器返回一个HTTP 302响应,这导致浏览器向GET指定的操作发出请求。因此,您看到的是一个以模型作为路由值的 get 请求。HTTP 不支持使用 POST 进行重定向,因此您无法更改它。

你可以做什么 - 从你的控制器调用方法而不返回浏览器(如果这是同一个控制器):

return AccountScreen(model);

您可以使用TempData来存储您的模型(这也将是 GET 请求,但模型不会在路由值中传递 - 它将存储在会话中)。在您的控制器中:

TempData["model"] = model;
return RedirectToAction("AccountScreen", "Customer");

在客户控制器中:

public ActionResult AccountScreen()
{
    YourModel model = TempData["model"] as YourModel;
    //...
}
于 2012-12-29T22:48:02.263 回答