ここではコントローラの定義は次のとおりです。
public class ProductController : Controller
{
//
// GET: /Product/
public ActionResult Index()
{
return View();
}
public ActionResult Help()
{
return View();
}
public ActionResult Details(int Id)
{
return View();
}
}
リターン・ビューで、アクションは()理解しました
それはのActionResultあり、ここで(ビューを返す)、実際に表示するためのHTMLページのコンテンツへの回帰であるが、のActionResultの多くの種類があります。
・表示() - するViewResultを返します。
・PartialView() - するPartialViewResultを返します。
・RedirectToAction() - RedirectToRouteResultを返します。
・リダイレクト() - RedirectResultを返します。
・コンテンツ() - ContentResultを返します。
・JSON() - 化するJsonResultを返します。
・ファイル() - FileResultを返します。
・JavaScriptの() - JavaScriptResultを返します。
・RedirectToRoute() - RedirectToRouteResultを返します。
説明:
・するViewResultは - 1つの通常のASP.NET MVCのビューを表します。
・するPartialViewResultは - ASP.NET MVCのビューのフラグメントを表します。
・RedirectResultは - 別のコントローラのアクションまたはURLにリダイレクトします。
return RedirectToAction("Index");
return RedirectToAction(“Index”, “Product”);
return RedirectToAction(“Details”, new {id=53});
・ContentResult - 基本的な例の.NET缶の種類限り、ブラウザにコンテンツのいくつかの基本的な型を表す送信:文字列、int型、ダブル、など
public string SayHello()
{
return "Hello";
}
・化するJsonResult - ブラウザの種類JSONに戻ります
public ActionResult List()
{
var quotes = new List<string>
{
"Look before you leap",
"The early bird gets the worm",
"All hat, no cattle"
};
return Json(quotes);
}
・FileResult - ダウンロードするファイルを返します
public ActionResult Download()
{
return File("~/Content/CompanyPlans.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "CompanyPlans.docx");
}
・EmptyResult - アクションは結果を返しません。
・HttpUnauthorizedResult - 返回HTTP不正なステータスコード。
・JavaScriptResultは - JavaScriptファイルを返します。
・RedirectToRouteResult - ルート値を使用しては、別のコントローラのアクションにリダイレクト
第二に、アクションを制御する方法と呼ばれています
1. AcceptVerbs
コードを見てください:
// GET: /Employee/Create
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Create()
{
return View();
}
// POST: /Employee/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Employee employeeToCreate)
{
try
{
_repository.InsertEmployee(employeeToCreate);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// DELETE: /Employee/Delete/1
[AcceptVerbs(HttpVerbs.Delete)]
public ActionResult Delete(int id)
{
_repository.DeleteEmployee(id);
return Json(true);
}
このアクションを作成したときにコントロールがトリガされていることをGETまたはPOST
HTTPサポートされているアクションタイプ:
・OPTIONS - 利用できる通信オプションについての情報を返します。
・GET - どんな情報戻り値は、要求によって識別されます。
・HEADは - メッセージ本文を返さずGETと同じ操作を実行します。
・POST - 新しい情報または更新既存の情報をポストします。
・PUT - 投稿新しい情報または情報を、既存のアップデート。
・削除 - 情報を削除します。
・TRACE - メッセージループバックを実行します。
・CONNECT - SSLトンネリングのために使用されます。
2. ACTIONNAME
[ActionName("Edit")]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Edit_GET(Merchandise merchandiseToEdit)
{
return View(merchandiseToEdit);
}
3. ActionMethodSelector
namespace MvcApplication1.Selectors
{
public class AjaxMethod : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
return controllerContext.HttpContext.Request.IsAjaxRequest();
}
}
}
コントローラーのコードでは:
[AjaxMethod]
[ActionName("Index")]
public string Index_AJAX()
{
var selectedIndex = _rnd.Next(_news.Count);
return _news[selectedIndex];
}
三、一个特殊处理:请求没有定义的Action的处理
public class CatalogController : Controller
{
public ActionResult Create()
{
return View();
}
public ActionResult Delete(int id)
{
return View();
}
protected override void HandleUnknownAction(string actionName)
{
ViewData["actionName"] = actionName;
View("Unknown").ExecuteResult(this.ControllerContext);
}
}
使用HandleUnkonwnAction、異常な状況下では見つかりません。デフォルトの戻り404リソースは、定義をオーバーライドすることができます
ます。https://www.cnblogs.com/dotLive/archive/2009/03/09/1406997.htmlで再現