Here is the definition of a Controller:
public class ProductController : Controller
{
//
// GET: /Product/
public ActionResult Index()
{
return View();
}
public ActionResult Help()
{
return View();
}
public ActionResult Details(int Id)
{
return View();
}
}
A, Action in return View () understood
That is ActionResult, here return View () is actually a return to the html page content to display, but there are many types of ActionResult:
· View () - Returns a ViewResult.
· PartialView () - Returns a PartialViewResult.
· RedirectToAction () - Returns a RedirectToRouteResult.
· Redirect () - Returns a RedirectResult.
· Content () - Returns a ContentResult.
· Json () - Returns a JsonResult.
· File () - Returns a FileResult.
· JavaScript () - Returns a JavaScriptResult.
· RedirectToRoute () - Returns a RedirectToRouteResult.
Explanation:
· ViewResult - represents one ordinary ASP.NET MVC view.
· PartialViewResult - represents a fragment of ASP.NET MVC view.
· RedirectResult - Redirects to another controller action or URL.
return RedirectToAction("Index");
return RedirectToAction(“Index”, “Product”);
return RedirectToAction(“Details”, new {id=53});
· ContentResult - send represent some basic types of content to the browser, as long as the basic type of .net can for example: string, int, double, etc.
public string SayHello()
{
return "Hello";
}
· JsonResult - a return to the browser type 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 - returns a file for download
public ActionResult Download()
{
return File("~/Content/CompanyPlans.docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "CompanyPlans.docx");
}
· EmptyResult - an action does not return results
· HttpUnauthorizedResult – 返回HTTP Unauthorized status code.
· JavaScriptResult - returns a JavaScript file.
· RedirectToRouteResult - using the route values redirect to another controller's action
Second, how to control the Action is called
1. AcceptVerbs
Look at the code:
// 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 or Post that control is triggered when the Create this Action
http Supported action types:
· OPTIONS – Returns information about the communication options available.
· GET – Returns whatever information is identified by the request.
· HEAD – Performs the same operation as GET without returning the message body.
· POST – Posts new information or updates existing information.
· PUT – Posts new information or updates existing information.
· DELETE – Deletes information.
· TRACE – Performs a message loop back.
· CONNECT – Used for SSL tunneling.
2. Use ActionName
[ActionName("Edit")]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Edit_GET(Merchandise merchandiseToEdit)
{
return View(merchandiseToEdit);
}
3. Use ActionMethodSelector
namespace MvcApplication1.Selectors
{
public class AjaxMethod : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
return controllerContext.HttpContext.Request.IsAjaxRequest();
}
}
}
In the Controller code:
[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);
}
}
Use HandleUnkonwnAction, default return 404 Resource Not Found under abnormal circumstances, you can override the definition
Reproduced in: https: //www.cnblogs.com/dotLive/archive/2009/03/09/1406997.html