「RedirectResult クラス (System.Web.Mvc)」の版間の差分
ナビゲーションに移動
検索に移動
imported>Administrator (ページの作成:「RedirectResultクラスとは、ASP.NET MVCのアクション・メソッドの戻り値(ActionResultクラスおよびその派生クラス)として...」) |
imported>Administrator (→使い方) |
||
8行目: | 8行目: | ||
using System.Web.Mvc; | using System.Web.Mvc; | ||
− | public class HomeController : Controller { | + | public class HomeController : Controller |
− | public ActionResult Index() { | + | { |
+ | public ActionResult Index() | ||
+ | { | ||
return new RedirectResult( "http://monobook.org/wiki/PHP" ); | return new RedirectResult( "http://monobook.org/wiki/PHP" ); | ||
} | } | ||
29行目: | 31行目: | ||
using System.Web.Mvc; | using System.Web.Mvc; | ||
− | public class HomeController : Controller { | + | public class HomeController : Controller |
− | public ActionResult Index() { | + | { |
+ | public ActionResult Index() | ||
+ | { | ||
return new RedirectResult( "http://monobook.org/wiki/PHP", true ); | return new RedirectResult( "http://monobook.org/wiki/PHP", true ); | ||
} | } |
2012年4月3日 (火) 01:48時点における版
RedirectResultクラスとは、ASP.NET MVCのアクション・メソッドの戻り値(ActionResultクラスおよびその派生クラス)として、Webブラウザに対して指定されたURIにリダイレクトするよう指示するHTTPレスポンスヘッダーを生成、送信するクラスである。
使い方
RedirectResultクラスの使い方はいたって簡単。 単純にリダイレクト先のURLを指定するだけである。 これでWebブラウザに対して302リダイレクトを送信する。
using System.Web.Mvc;
public class HomeController : Controller
{
public ActionResult Index()
{
return new RedirectResult( "http://monobook.org/wiki/PHP" );
}
}
これはPHPでいう以下のコードに相当する。
<?php
header( "Location: http://monobook.org/wiki/PHP" );
301リダイレクト
ASP.NET MVC 3では、bool型のPermanentプロパティが追加され、永続的に移転したことを示す永久リダイレクト(301リダイレクト)を発生させるかを指定できるようになった。
なお、ASP.NET MVC 3で追加されたものでありASP.NET MVC 2以前では存在しないので注意。
using System.Web.Mvc;
public class HomeController : Controller
{
public ActionResult Index()
{
return new RedirectResult( "http://monobook.org/wiki/PHP", true );
}
}
これはPHPでいう以下のコードに相当する。
<?php
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: http://monobook.org/wiki/PHP" );
関連項目
- ActionResult クラス (System.Web.Mvc) - アクションの戻り値
- ASP.NET MVC
参考文献