2013-06-12 77 views
3

如何在ASP MVC 4中重定向页面?在没有302状态码的ASP MVC 4中重定向

我试过Redirect,RedirectPermanent,RedirectToAction等等。但它就像HTTP重定向(使用http-equiv refresh),而不是标题重定向。它将首先显示标题状态302,然后重定向。我想头球像php header('location:/');

 

[HttpGet] 
public ActionResult SignOut() 
{ 
    UtilsHelper.NoCache(); 
    session.Destroy(); 
    return RedirectToAction("Index", "Home"); 
} 
+0

直接调用控制器方法。有关示例,请参阅[这里](http://stackoverflow.com/questions/1296680/net-mvc-call-method-on-different-controller)。 –

+0

但是,我想重定向,而不是显示来自不同控制器的方法的结果。 –

+3

不,你不知道。如果你真的想重定向,你会和302一起生活。它的语义是正确的。 –

回答

3

重定向如果你真的担心重定向代码,你可以在一个较低的水平控制。

[HttpGet] 
public ActionResult SignOut() 
{ 
    UtilsHelper.NoCache(); 
    session.Destroy(); 
    HttpContext.Response.Clear(); 
     HttpContext.Response.StatusCode = 302; //change it to 301? 
     HttpContext.Response.RedirectLocation = "/"; 
     HttpContext.Response.End(); 
} 

我不知道302这里有什么问题吗?

+0

返回值呢? –

+0

@BiasTegaralaga应该返回的东西,但它会首先重定向,所以返回值无关紧要。 – Adrian