すべての機会のActionResult

森の奥に行くほど、MVCフレームワークは厚くなります。 ここで先日、2番目のバージョンのプレビューが来ました。 可能性にすぐに慣れ、私は非常に喜んでいました。 しかし、その後、MS MVCにはまだ(計画中でさえも)存在しないシンプルで非常に興味深いRoR機能について思い出しました。
Actionメソッドの「拡張子」を指定し、それによってビューのタイプを決定する機能について話している。


始めるには、例を考えてください。 リクエストht_p:// localhost / Blog / Comments / 0は、ブログの記事番号0に対するコメントのリストを含むhtmlページを返します。 この場合、Blogはコントローラーの名前、Commentsはメソッドの名前、0は投稿IDです。
コントローラのコードは次のようになります。

public class BlogController : Controller
{
public ActionResult Comments( int id)
{
var strings = new [] { "" , "" , "" }; // ,
return View(strings);
}
}

* This source code was highlighted with Source Code Highlighter .


ここではすべてがシンプルです!

しかし、htmlページに加えてjsonでシリアル化されたコメントのリストを取得する必要がある場合はどうでしょうか。 最も簡単な方法は、別のメソッドを作成し、CommentsJsonなどの名前を付け、Json()メソッドを明示的に呼び出すことです。 たとえば、次のように:

public ActionResult CommentsJson( int id)
{
var strings = new [] { "" , "" , "" }; // ,
return Json(strings);
}


* This source code was highlighted with Source Code Highlighter .


これで、ht_pでjsonリストを取得できます。// localhost / Blog / CommentsJson / 0

問題は解決しましたが、同意しませんか? 率直に言って、このコードは私には向いていません。 第一に、複製、第二に、後で使用する必要があるURLが好きではありません。第三に、他のメソッドでスキームを使用する方法がありません。

この場合、独自のActionResultを作成する機会が役立ちます。 「額に」問題を解決しようとします。 Commentsメソッドを次のように変更します。

public class BlogController : Controller
{
public ActionResult Comments( int id)
{
var strings = new [] { "" , "" , "" }; // ,
return new ComplexResult
{
Json = () => Json(strings),
View = () => View(strings)
};
}
}

* This source code was highlighted with Source Code Highlighter .


ご覧のとおり、ComplexResultクラスはActionResultから継承する必要があります。
次のように実装してみましょう。

public class ComplexResult : ActionResult
{
public override void ExecuteResult(ControllerContext context)
{
object value ;
if (context.RouteData.Values.TryGetValue( "result" , out value ))
{
var result = value .ToString();
if (result.Equals( "json" , StringComparison.InvariantCultureIgnoreCase))
{
Json().ExecuteResult(context);
return ;
}
}

View().ExecuteResult(context);
}

public Func<JsonResult> Json { get ; set ; }

public Func<ViewResult> View { get ; set ; }
}


* This source code was highlighted with Source Code Highlighter .


ここで説明する必要があります。結果を処理するとき、リクエストの解析時に取得した値のリストに文字列「result」が存在することを確認します。 ユーザーが「json」という行を送信し、ユーザーが事前定義したJsonResultを実行していることを確認します。 行が見つからないか、「json」と等しくない場合、ViewResultを起動します。 ロジックは非常に単純です。

これと並行して、ルート設定に次の行を追加します。

public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute( "{resource}.axd/{*pathInfo}" );

// route default
routes.MapRoute( "Result" , "{controller}/{action}.{result}/{id}" ,
new { controller = "Home" , action = "Index" , id = "" , result = "view" });

routes.MapRoute( "Default" , "{controller}/{action}/{id}" ,
new { controller = "Home" , action = "Index" , id = "" });
}

* This source code was highlighted with Source Code Highlighter .


これで、ht_p://localhost/Blog/Comments.json/0クエリ文字列で明示的に指定することで、jsonオブジェクトを取得できます。 またはht_p:// localhost / Blog / Comments / 0-htmlが必要な場合。 そして最も重要なことは、繰り返しメソッドでコードを詰まらせることなく、他のメソッドでComplexResultを簡単に使用できることです。

Fantasyは、同様に、他の標準または非標準(XmlResult、RssResultなど)ActionResultsをComplexResultに追加できることを示唆しています。 ASP.NET MVCの将来のバージョンでは、おそらくこれが私たちを待っています。

コードは書かれましたが、今は言い訳です:このメモで書かれたコードは、MVCフレームワークの機能を理解するためにのみ使用することをお勧めします。それは安全ではないことは明らかです。

ご清聴ありがとうございました。

Source: https://habr.com/ru/post/J66355/


All Articles