際際滷

際際滷Share a Scribd company logo
Controller
惘忰 惘惷悋
惠悋惡愕惠悋95
Rtwo.ir
Controller in asp.net mvc
惡忰惓 惆
錫惡悋 悛愆悋controller
錫惡悋 悛愆悋Routing
錫惡悋 悛愆悋Filter悋
錫惡悋 悛愆悋Area
錫愆惠 惡 愆惘惺controller
愀悋惡 惠惠惘
ControllerAction愕惠
悋 悽惘悴 悋悋惺Action悋
愕惘悋惡Action悋
奄擧惆悋 惘愕惆  惆惘悽悋愕惠 惠action愆惆  悋惠悽悋惡 悋悴惘悋 惡惘悋
奄悋惆 擯 悋 愕惘悋惡 惆惘 悋 悋惠
愆惆  惠惺惘 擯  愕惠 惠惘
悋 惠惘 悋悋惺
Configuring Filters--->dot net core
Authorization Filters
Resource Filters--->dot net core
Action Filters
Exception Filters
Result Filters
惡悋 悛愆悋Area
ControllerAction愕惠
Action惘 惡悋愆惆  惠悋惡惺Action 拆悋愕悽 惘惘擯惘 愕惠 悋慍 惘愕惆 惆惘悽悋愕惠 擧 惡
惆惆.
錫惆惘ASP.NET MVCController愀 惡惆 惆愕惠 惡惘悋 惡悋愆惆  擧悋愕 
悋慍 擯惘Action悋.
錫擧悋惘 忰慍 悋愕悋愕 惡惘 悋 惡惆 惆愕惠 悋action悋 悋惺悋 悋惡惠  惡悋愆惆  悋
惡 悴惺 惆愕惠Action惆惆  惘悋 悋悋惆擧惠惘  擧愆  愕惘悋惡 惡惘悋 悋
惆愕惠惘愕
錫拆愆 惆惘 悋 擧惠惘 悋Controllers擯惘惆  惘悋惘.
錫擧悋愕 悋慍 擧惠惘惘 擧悋愕Microsoft.AspNetCore.Mvc.Controller惡惘惆  悋惘惓.
錫惡 惡悋惆 擧惠惘惘 擧悋愕 悋Controller愆惆 悽惠悋惆AccountController
HTTP requests verb
愀惘 悋慍 擧悋惠 悋 惆惘悽悋愕惠HTTP verb愆惆  愆悽惶 悋.
悋悋慍 悋 惆惘悽悋愕惠愕惘惘悋悴悋 惡惘悋愆悋 惘悋 擧悋惘 惠悋 悽悋惆 惆惆
悋悋惺verb悋:
 GET
 POST
 PUT
 DELETE
 Head
 Trace
 OPTIONS


擧 惘 惡惘悋 慍 擧愕悋 悛惆惘愕 擧 惆惘
悋慍http verb惡惘悋 悋擧愆 擧 惡悋惆 悋
惡悋愆惆 悴惆 拆悋愕悽擯
悋 悽惘悴 悋悋惺Action
悋惡 愕惘
悋惡 愕惘action悋
http://zanjandotnet.ir/home/index
First segment Second segment
悽悋  愕惘悋惡 悋慍 擧 悋 悽惘悴  悋 悛惆惘愕
URL愆惆 惆惘悽悋愕惠 Action悋惠悽悋惡
http://example.com/Users/Edit/5 UsersController.Edit(5)
http://example.com/Users/Edit UsersController.Edit()
http://example.com/Users UsersController.Index()
http://example.com HomeController.Index()
惡 悋慍 惘悋 悋慍 惘惆 悛惆惘愕悋 惡悋惆惡擧 惠惺惘愕惘悋惡 惠悋惡惠
惡 惘悋 惆惘悽悋愕惠Action惆惆 惠忰 惆惘愕惠
愕惘悋惡 悋 惠惺惘
RouteConfig.cs
Global.asax.cs
擧惆  擧悋惘 擯 悋惡 愕惘
{controller} /{action}
home/index
anything/willdo
=============================
abc{controller} / {action}
abchome / whatever
==================
abc{controller} / {action}
home / whatever
愆惆 惠惺惘 愕惘悋 惆惘 悋惠
RouteData
悴惺Key Value擧惆  悋悴悋惆 愕惘悋惡 擧 悋
惠愕愀 悴惺 悋model binder擯惘惆  惘悋惘 悋愕惠悋惆 惘惆 擧惠惘惘 悽惆 悋
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}束
);
product/index/3
Variable value
controller Product
action Index
id 3
RouteData
惘惷 拆愆 悋惆惘
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id=3 }
);
product/index
Variable Value
controller Product
action Index
Id 3
悋悽惠悋惘 悋惆惘
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {action = "Index, id=UrlParameter.Optinal}
);
 product/index/3
 product/index
Variable value
controller Product
action Index
id 3
Variable value
controller Product
action index
Namespace愕惘悋惡 惆惘
IgnoreRoute
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
404
惠惘
擧惆 惆愕惠擧悋惘
惠惘愕惠
惠惘悋悴惘悋 悴惠 惘悋 惡惘悋 愀 惠悋惆 愆悋 擧 悋愕惠 愕悋惘愆 擧悋愕 擧 惡 悋 惡悋慍 惺惆
悋悴惘悋擧Action悋惆 愕悋慍 拆悋惆 悛 惆惘.
 惺惘 擯 惠惘愆惆
奄惘愆 惡Attribute惘 惡惘Action悋 悋Controller悋
奄愕惘悋愕惘 惡惶惘惠
愆擧 惡 悋愕惠悋惆attribute
[HandleError]
[Authorize]
public class CourseController : Controller
{
[OutputCache]
[RequireHttps]
public ActionResult Net( string name )
{
ViewBag.Course = BL.GetCourse(name);
return View();
}
}
惺惘悋 惆惘 愕惘悋愕惘 惡惶惘惠Global.asax
GobalFilters.Filters.Add(new AuthorizationFilterA() { Order = 2});
悋惠  悋悋惺惠惘悋
.1Authorization filters:悋惆惘 惆擯惘 惠惘 悋 惠惆 惘 悋悴惘悋 悋慍 拆愆 惠惘悋 惺
擧惆 惘悋 惘悋悛 悋悴惘悋 愃 悋擧悋  愆惆 悋悴惘悋 悴悋惘 擧惠惘惘.
.2Action filters:拆愆悋慍 拆愕悋悴惘悋拆愆   愆惆 悋悴惘悋 悴悋惘 擧惠惘惘 惠惆悋
惠惆悋 悋 悽惘悴 悋惘悋悧 悋慍.擧 惘惆惘 忰 惠悋 惓悋 惡惘悋 惠惘惠惡 悋 惡View擧惠惘 惠忰惠 惘悋
擯惘惠
.3Result filters:惡 愆惡 惡愕悋惘ActionFilter拆愆 惠悋 擧 惠悋惠 悋 惡悋 悋愕惠悋慍
拆愕   惠惆 惠悴 惡悋慍擯惘惆悋惆悋慍擯惘惆惆 惘悋悽悋 惠惆 悋悴惘悋
.4Exception filters:愆惆 悽悋惆 悋悴惘悋 悋惘 惆擯惘 惠惘悋 惠悋 悋悴惘悋 悋慍 拆愕
悽惘 悋 悋愕惠 惆悋惆 惘悽 悋愕惠惓悋 惡 悋 惆惘 悛悋 悋擧 悋慍 惶惘惴惘
惆惘 悋惠惺  惠惘悋
public class HomeController : Controller
{
[AuthorizationFilterA(Order = 2)]
[AuthorizationFilterB(Order = 1)]
public ActionResult Index()
{
return View();
}
}
悋悴惘悋 惠惘惠惡
惆悽悋 悋 惠惘
惆悽悋 惠惘 悋慍惆 忰
奄悋 惘愆:擧悋愕 悋慍 惡惘 悋惘惓  惠惘悋 悋悋惺 悋慍 擧 悋惠惘愕 愕悋慍 拆悋惆FilterAttribute
奄惆 惘愆:悋慍 惡惘 悋惘惓ActionFilterAttribute
奄愕 惘愆:擧惠惘惘 惆悋悽 愕悋慍 拆悋惆
奄悋惘 惘愆:惆惘 悋  惠擧悋惘 惠惘悋 擧悋愕 悋慍 惡惘 悋惘惓MVC悛 惠惆悋 惠忰惘
Controller in asp.net mvc
Authorization Filter
Action Filter
Result Filter
Exception Filter
惓悋
public class MyExceptionAttribute: FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (!filterContext.ExceptionHandled &&
filterContext.Exception is NullReferenceException)
{
filterContext.Result =
new RedirectResult("/MyError.html");
filterContext.ExceptionHandled = true;
}
}
}
Area
擧惆 惡 惡悽愆 惴盒潮
Areas
public class AdminAreaRegistration : AreaRegistration {
public override string AreaName {
get { return "Admin"; }
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
Areas
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);

More Related Content

Controller in asp.net mvc

Editor's Notes

  1. To be clear, it is not that the value of id is null when no corresponding segment is supplied; rather, the case is that an id variable is not defined To distinguish if user sent a value or not Separation of concerns (defaults in routing?)