mirror of
https://github.com/rudollee/LearningMVC.git
synced 2026-08-11 16:32:55 +00:00
Ch. 17 Controllers and Actions
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
|
||||
namespace ControllersAndActions.Controllers
|
||||
{
|
||||
public class BasicController : IController
|
||||
{
|
||||
public void Execute(RequestContext requestContext)
|
||||
{
|
||||
string controller = (string)requestContext.RouteData.Values["controller"];
|
||||
string action = (string)requestContext.RouteData.Values["actionn"];
|
||||
|
||||
requestContext.HttpContext.Response.Write(string.Format("Controller: {0}, Action: {1}", controller, action));
|
||||
|
||||
if (action.ToLower() == "redirect")
|
||||
{
|
||||
requestContext.HttpContext.Response.Redirect("/Derived/Index");
|
||||
}
|
||||
else
|
||||
{
|
||||
requestContext.HttpContext.Response.Write(string.Format("Controller: {0}, Action: {1}", controller, action));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using ControllersAndActions.Infrastructure;
|
||||
|
||||
namespace ControllersAndActions.Controllers
|
||||
{
|
||||
public class DerivedController : Controller
|
||||
{
|
||||
public ActionResult Index()
|
||||
{
|
||||
ViewBag.Message = "Hello from the DerivedController Index method";
|
||||
return View("MyView");
|
||||
}
|
||||
|
||||
//public void ProduceOutput()
|
||||
//{
|
||||
// if (Server.MachineName == "TINY")
|
||||
// {
|
||||
// Response.Redirect("/Basic/Index");
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Response.Write("Controller: Derived, Action: ProduceOutput");
|
||||
// }
|
||||
//}
|
||||
|
||||
public ActionResult ProduceOutput()
|
||||
{
|
||||
if (Server.MachineName == "TINY")
|
||||
{
|
||||
return new CustomRedirectResult { Url = "/Basic/Index" };
|
||||
}
|
||||
else
|
||||
{
|
||||
Response.Write("Controller: Derived, Action: ProduceOutput");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public ActionResult RenameProduct()
|
||||
{
|
||||
//Request.QueryString
|
||||
//Request.Form
|
||||
//Request.Cookies
|
||||
//Request.HttpMethod
|
||||
//Request.Headers
|
||||
//Request.Url
|
||||
//Request.UserHostAddress
|
||||
//RouteData.Route
|
||||
//RouteData.Values
|
||||
//HttpContext.Application
|
||||
//HttpContext.Cache
|
||||
//HttpContext.Items
|
||||
//HttpContext.Session
|
||||
//User
|
||||
//TempData
|
||||
|
||||
string userName = User.Identity.Name;
|
||||
string serverName = Server.MachineName;
|
||||
string clientIP = Request.UserHostAddress;
|
||||
DateTime dateStamp = HttpContext.Timestamp;
|
||||
|
||||
string oldProductName = Request.Form["OldName"];
|
||||
string newProductName = Request.Form["NewName"];
|
||||
|
||||
//ViewData["RenameResult"] = result;
|
||||
return View("ProductRenamed");
|
||||
}
|
||||
|
||||
public ActionResult ShowWeatherForecast(string city, DateTime forDate)
|
||||
{
|
||||
//string city = (string)RouteData.Values["city"];
|
||||
//DateTime forDate = DateTime.Parse(Request.Form["forDate"]);
|
||||
return View();
|
||||
}
|
||||
|
||||
public ActionResult Search(string query = "all", int page = 1)
|
||||
{
|
||||
// ...
|
||||
return View();
|
||||
}
|
||||
|
||||
public ActionResult Refer()
|
||||
{
|
||||
// inner type of ActionResult
|
||||
//ActionResult
|
||||
//PartialView
|
||||
//RedirectToRoute
|
||||
//RedirectResult
|
||||
//ContentResult
|
||||
//FileResult
|
||||
//JsonResult
|
||||
//JavaScriptResult
|
||||
//HttpUnauthorizedResult
|
||||
//HttpNotFoundResult
|
||||
//HttpStatusCodeResult
|
||||
//EmptyResult
|
||||
|
||||
return View();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace ControllersAndActions.Controllers
|
||||
{
|
||||
public class ExampleController : Controller
|
||||
{
|
||||
public ViewResult Index()
|
||||
{
|
||||
//return View("Homepage");
|
||||
//return View("Index", "_AlternateLayoutPage");
|
||||
//return View("~/Views/Other/Index.cshtml");
|
||||
|
||||
DateTime date = DateTime.Now;
|
||||
//return View(date);
|
||||
|
||||
ViewBag.Message = "Hello";
|
||||
ViewBag.Date = DateTime.Now;
|
||||
ViewBag.TTT = "TestTestTest";
|
||||
return View();
|
||||
}
|
||||
|
||||
//public RedirectResult Redirect()
|
||||
//{
|
||||
// return Redirect("/Example/Index");
|
||||
// //return RedirectPermanent("/Example/Index");
|
||||
//}
|
||||
|
||||
public RedirectToRouteResult Redirect()
|
||||
{
|
||||
//return RedirectToRoute(new { controller = "Example", Action = "Index", ID = "MyID" });
|
||||
return RedirectToAction("Index");
|
||||
//return RedirectToActionPermanent("Index");
|
||||
//return RedirectToAction("Index", "Basic"); // to other controller
|
||||
}
|
||||
|
||||
public RedirectToRouteResult RedirectToRoute()
|
||||
{
|
||||
TempData["Message"] = "Hello";
|
||||
TempData["Date"] = DateTime.Now;
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
public HttpStatusCodeResult StatusCode()
|
||||
{
|
||||
//return new HttpStatusCodeResult(404, "URL cannot be serviced");
|
||||
return HttpNotFound();
|
||||
//return new HttpUnauthorizedResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user