我正在尝试将其用于登录页面。
if (Session["UserID"] == null)
     Server.Transfer("/Account/Login", true);
但是我得到了异常->执行子请求/帐户/登录时出错。
我正在尝试将其用于登录页面。
if (Session["UserID"] == null)
     Server.Transfer("/Account/Login", true);
但是我得到了异常->执行子请求/帐户/登录时出错。
要使用服务器传输方法,您可以从 Simon Weaver 那里查看,但在您的问题的上下文中,我会使用重定向操作。
RedirectToAction(new {
   controller="Account", 
   action="Login"
});
让它告诉登录控制器回到哪里尝试
RedirectToAction( new {
   controller="Account",
   action="Login",
   new RouteValueDictionary { 
      {"actionToGoBackTo", "theActionName"},
      {"controllerToGoBackTo", "theControllerName"}
   }); 
请注意,登录操作需要使用两个字符串参数,actionToGoBackTo 和 controllerToGoBackTo。
您应该在 Server.Transfer 中获得与您想要的完全相同的结果。
public ActionResult Index() {
    ......
      var url = "/MyContoller?param=xxx";
      Server.TransferRequest(url, true);
      return new EmptyResult();
}