差分

ナビゲーションに移動 検索に移動

RedirectResult クラス (System.Web.Mvc)

852 バイト追加, 2012年4月3日 (火) 02:11
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: http://monobook.org/wiki/PHP" );
</source>
 
[[ASP.NET MVC 2]]以前で301リダイレクトをしたい場合はActionResultクラスを継承した自前クラスを作る。
<source lang="csharp">
using System.Web.Mvc;
 
public class PermanentRedirectResult : ActionResult
{
public string Url { get; set; }
 
public PermanentRedirectResult(string url)
{
this.Url = url;
}
 
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (string.IsNullOrEmpty(this.Url))
{
throw new ArgumentException("url is null or empty", "Url");
}
 
context.HttpContext.Response.StatusCode = 301;
context.HttpContext.Response.RedirectLocation = this.Url;
context.HttpContext.Response.End();
}
}
</source>
匿名利用者

案内メニュー