「RedirectResult クラス (System.Web.Mvc)」の版間の差分
ナビゲーションに移動
検索に移動
imported>Administrator |
imported>Administrator |
||
(同じ利用者による、間の5版が非表示) | |||
1行目: | 1行目: | ||
− | RedirectResultクラスとは、[[ASP.NET MVC]]の[[アクション・メソッド]]の戻り値([[ActionResultクラス]]およびその派生クラス)として、[[Webブラウザ]] | + | RedirectResultクラスとは、[[ASP.NET MVC]]の[[アクション・メソッド]]の戻り値([[ActionResultクラス]]およびその派生クラス)として、[[Webブラウザ]]に対して指定された[[URI]]に[[リダイレクト]]するよう指示するHTTPレスポンスヘッダーを生成、送信する[[クラス]]である。 |
== 使い方 == | == 使い方 == | ||
RedirectResultクラスの使い方はいたって簡単。 | RedirectResultクラスの使い方はいたって簡単。 | ||
− | + | 単純に[[リダイレクト]]先の[[URL]]を指定するだけである。 | |
− | + | これで[[Webブラウザ]]に対して[[302リダイレクト]]を送信する。 | |
<source lang="csharp"> | <source lang="csharp"> | ||
using System.Web.Mvc; | using System.Web.Mvc; | ||
24行目: | 24行目: | ||
=== 301リダイレクト === | === 301リダイレクト === | ||
− | [[ASP.NET MVC 3]] | + | [[ASP.NET MVC 3]]では、[[bool型]]のPermanentという[[プロパティ]]が追加され、永続的に移転したことを示す永久リダイレクト([[301リダイレクト]])を発生させるかを指定できるようになった。Permanentプロパティの値はRedirectResultクラスの[[コンストラクタ]]の2番目の[[引数]]として指定することもできる。 |
なお、[[ASP.NET MVC 3]]で追加されたものであり[[ASP.NET MVC 2]]以前では存在しないので注意。 | なお、[[ASP.NET MVC 3]]で追加されたものであり[[ASP.NET MVC 2]]以前では存在しないので注意。 | ||
35行目: | 35行目: | ||
public ActionResult Index() | public ActionResult Index() | ||
{ | { | ||
− | // | + | // ASP.NET MVC 3でコンストラクタに第2引数(bool型)が追加され、 |
+ | // trueにすると301リダイレクトになる。 | ||
return new RedirectResult( "http://monobook.org/wiki/PHP", true ); | return new RedirectResult( "http://monobook.org/wiki/PHP", true ); | ||
} | } | ||
46行目: | 47行目: | ||
header( "HTTP/1.1 301 Moved Permanently" ); | header( "HTTP/1.1 301 Moved Permanently" ); | ||
header( "Location: http://monobook.org/wiki/PHP" ); | 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> | </source> | ||
2012年7月11日 (水) 10:35時点における最新版
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リダイレクト)を発生させるかを指定できるようになった。Permanentプロパティの値はRedirectResultクラスのコンストラクタの2番目の引数として指定することもできる。
なお、ASP.NET MVC 3で追加されたものでありASP.NET MVC 2以前では存在しないので注意。
using System.Web.Mvc;
public class HomeController : Controller
{
public ActionResult Index()
{
// ASP.NET MVC 3でコンストラクタに第2引数(bool型)が追加され、
// trueにすると301リダイレクトになる。
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" );
ASP.NET MVC 2以前で301リダイレクトをしたい場合はActionResultクラスを継承した自前クラスを作る。
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();
}
}
関連項目[編集 | ソースを編集]
- ActionResult クラス (System.Web.Mvc) - アクションの戻り値
- ASP.NET MVC
参考文献[編集 | ソースを編集]