{"id":128,"date":"2010-07-27T19:25:39","date_gmt":"2010-07-27T19:25:39","guid":{"rendered":"76dc611d6ebaafc66cc0879c71b5db5c"},"modified":"2010-07-27T19:25:39","modified_gmt":"2010-07-27T19:25:39","slug":"","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/128","title":{"rendered":"ASP.NET MVC \u7406\u89e3"},"content":{"rendered":"<h2><span style=\"font-size: 12pt\">\u4e00.\u6458\u8981<\/span><br \/>\n<\/h2>\n<p><span style=\"font-size: 12pt\">\u4e00\u4e2aUrl\u8bf7\u6c42\u7ecf\u8fc7\u4e86Routing\u5904\u7406\u540e\u4f1a\u8c03\u7528Controller\u7684Action\u65b9\u6cd5. \u4e2d\u95f4\u7684\u8fc7\u7a0b\u662f\u600e\u6837\u7684? Action\u65b9\u6cd5\u4e2d\u8fd4\u56deActionResult\u5bf9\u8c61\u540e,\u5982\u4f55\u5230\u8fbeView\u7684? \u672c\u6587\u5c06\u8bb2\u89e3Controller\u7684\u57fa\u672c\u7528\u6cd5,&nbsp; \u6df1\u5165\u5206\u6790Controller\u7684\u8fd0\u884c\u673a\u5236, \u5e76\u4e14\u63d0\u4f9b\u4e86\u521b\u5efa\u6240\u6709\u7c7b\u578bAction\u7684\u4ee3\u7801. \u503c\u5f97\u5b66\u4e60ASP.NET MVC\u65f6\u53c2\u8003.<\/span>\n<\/p>\n<h2><span style=\"font-size: 12pt\">\u4e8c.\u627f\u4e0a\u542f\u4e0b<\/span><br \/>\n<\/h2>\n<p><span style=\"font-size: 12pt\">\u5728\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d, \u6211\u5df2\u7ecf\u5b66\u4f1a\u4e86\u5982\u4f55\u4f7f\u7528Routing\u83b7\u53d6Controller\u548cAction, \u968f\u540e\u7684\u7a0b\u5e8f\u4f1a\u8c03\u7528Controller\u4e2d\u7684Action\u65b9\u6cd5.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u6bcf\u4e2aAction\u65b9\u6cd5\u90fd\u8981\u8fd4\u56de\u4e00\u4e2aActionResult\u5bf9\u8c61. \u4e00\u4e2aAction\u4f1a\u5c06\u6570\u636e\u4f20\u9012\u7ed9View,\u5982\u56fe:<\/span>\n<\/p>\n<p><a href=\"http:\/\/images.cnblogs.com\/cnblogs_com\/zhangziqiu\/WindowsLiveWriter\/3903880e4390_7859\/image_2.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" alt=\"image\" border=\"0\" height=\"228\" src=\"http:\/\/images.cnblogs.com\/cnblogs_com\/zhangziqiu\/WindowsLiveWriter\/3903880e4390_7859\/image_thumb.png\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" title=\"image\" width=\"529\" \/><\/a>\n<\/p>\n<h2><span style=\"font-size: 12pt\">\u4e09.Controller\u4e0eAction\u7684\u4f5c\u7528<\/span><br \/>\n<\/h2>\n<h3><span style=\"font-size: 12pt\">1.\u804c\u8d23<\/span><br \/>\n<\/h3>\n<p><span style=\"font-size: 12pt\">Controller\u8d1f\u8d23\u5c06\u83b7\u53d6Model\u6570\u636e\u5e76\u5c06Model\u4f20\u9012\u7ed9View\u5bf9\u8c61.\u901a\u77e5View\u5bf9\u8c61\u663e\u793a.<\/span>\n<\/p>\n<h3><span style=\"font-size: 12pt\">2.ASP.NET MVC\u4e2d\u7684Controller\u548cAction<\/span><br \/>\n<\/h3>\n<p><span style=\"font-size: 12pt\">\u5728ASP.NET MVC\u4e2d, \u4e00\u4e2aController\u53ef\u4ee5\u5305\u542b\u591a\u4e2aAction. \u6bcf\u4e00\u4e2aAction\u90fd\u662f\u4e00\u4e2a\u65b9\u6cd5, \u8fd4\u56de\u4e00\u4e2aActionResult\u5b9e\u4f8b. <\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">ActionResult\u7c7b\u5305\u62ecExecuteResult\u65b9\u6cd5, \u5f53ActionResult\u5bf9\u8c61\u8fd4\u56de\u540e\u4f1a\u6267\u884c\u6b64\u65b9\u6cd5. <\/span>\n<\/p>\n<h4><span style=\"font-size: 12pt\">\u4e0b\u9762\u5206\u5c42\u6b21\u7684\u603b\u7ed3Controller \u5904\u7406\u6d41\u7a0b:<\/span><br \/>\n<\/h4>\n<h4><span style=\"font-size: 12pt\">1. \u9875\u9762\u5904\u7406\u6d41\u7a0b<\/span><br \/>\n<\/h4>\n<p><span style=\"font-size: 12pt\">\u53d1\u9001\u8bf7\u6c42 &ndash;&gt; UrlRoutingModule\u6355\u83b7\u8bf7\u6c42 &ndash;&gt; MvcRouteHandler.GetHttpHandler() &ndash;&gt; <\/span><br \/>\n<strong style=\"font-size: 12pt\">MvcHandler.ProcessRequest()<\/strong>\n<\/p>\n<h4><span style=\"font-size: 12pt\">2.MvcHandler.ProcessRequest() \u5904\u7406\u6d41\u7a0b:<\/span><br \/>\n<\/h4>\n<p><span style=\"font-size: 12pt\">\u4f7f\u7528\u5de5\u5382\u65b9\u6cd5\u83b7\u53d6\u5177\u4f53\u7684Controller &ndash;&gt; <\/span><br \/>\n<strong style=\"font-size: 12pt\">Controller.Execute()<\/strong><br \/>\n &ndash;&gt; \u91ca\u653eController\u5bf9\u8c61<\/p>\n<h4><span style=\"font-size: 12pt\">3.Controller.Execute() \u5904\u7406\u6d41\u7a0b<\/span><br \/>\n<\/h4>\n<p><span style=\"font-size: 12pt\">\u83b7\u53d6Action &ndash;&gt; \u8c03\u7528Action\u65b9\u6cd5\u83b7\u53d6\u8fd4\u56de\u7684ActionResult &ndash;&gt; \u8c03\u7528<\/span><br \/>\n<strong style=\"font-size: 12pt\">ActionResult.ExecuteResult()<\/strong><br \/>\n \u65b9\u6cd5<\/p>\n<h4><span style=\"font-size: 12pt\">4.ActionResult.ExecuteResult() \u5904\u7406\u6d41\u7a0b<\/span><br \/>\n<\/h4>\n<p><span style=\"font-size: 12pt\">\u83b7\u53d6IView\u5bf9\u8c61-&gt; \u6839\u636eIView\u5bf9\u8c61\u4e2d\u7684\u9875\u9762\u8def\u5f84\u83b7\u53d6Page\u7c7b-&gt; \u8c03\u7528<\/span><br \/>\n<strong style=\"font-size: 12pt\">IView.RenderView()<\/strong><br \/>\n \u65b9\u6cd5(\u5185\u90e8\u8c03\u7528Page.RenderView\u65b9\u6cd5)<\/p>\n<p><span style=\"font-size: 12pt\">\u901a\u8fc7\u5bf9MVC\u6e90\u4ee3\u7801\u7684\u5206\u6790,\u6211\u4eec\u4e86\u89e3\u5230Controller\u5bf9\u8c61\u7684\u804c\u8d23\u662f\u4f20\u9012\u6570\u636e,\u83b7\u53d6View\u5bf9\u8c61(\u5b9e\u73b0\u4e86IView\u63a5\u53e3\u7684\u7c7b),\u901a\u77e5View\u5bf9\u8c61\u663e\u793a.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">View\u5bf9\u8c61\u7684\u4f5c\u7528\u662f\u663e\u793a.\u867d\u7136\u663e\u793a\u7684\u65b9\u6cd5RenderView()\u662f\u7531Controller\u8c03\u7528\u7684,\u4f46\u662fController\u4ec5\u4ec5\u662f\u4e00\u4e2a&quot;\u6307\u6325\u5b98&quot;\u7684\u4f5c\u7528, \u5177\u4f53\u7684\u663e\u793a\u903b\u8f91\u4ecd\u7136\u5728View\u5bf9\u8c61\u4e2d.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u9700\u8981\u6ce8\u610fIView\u63a5\u53e3\u4e0e\u5177\u4f53\u7684ViewPage\u4e4b\u95f4\u7684\u8054\u7cfb.\u5728Controller\u548cView\u4e4b\u95f4\u8fd8\u5b58\u5728\u7740IView\u5bf9\u8c61.\u5bf9\u4e8eASP.NET\u7a0b\u5e8f\u63d0\u4f9b\u4e86WebFormView\u5bf9\u8c61\u5b9e\u73b0\u4e86IView\u63a5\u53e3.WebFormView\u8d1f\u8d23\u6839\u636e\u865a\u62df\u76ee\u5f55\u83b7\u53d6\u5177\u4f53\u7684Page\u7c7b,\u7136\u540e\u8c03\u7528Page.RenderView().<\/span>\n<\/p>\n<h2><span style=\"font-size: 12pt\">\u56db.ActionResult\u89e3\u6790<\/span><br \/>\n<\/h2>\n<p><span style=\"font-size: 12pt\">\u901a\u8fc7\u4e0a\u9762\u7684\u6d41\u7a0b,\u6211\u4eec\u77e5\u9053\u4e86ActionResult\u5bf9\u8c61\u5728\u6574\u4e2a\u6d41\u7a0b\u4e2d\u7684\u4f5c\u7528.ActionResult\u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b, \u5728Action\u4e2d\u8fd4\u56de\u7684\u90fd\u662f\u5176\u6d3e\u751f\u7c7b.\u4e0b\u9762\u662f\u6211\u6574\u7406\u7684ASP.NET MVC 1.0 \u7248\u672c\u4e2d\u63d0\u4f9b\u7684ActionResult\u6d3e\u751f\u7c7b:<\/span>\n<\/p>\n<p>&nbsp;<\/p>\n<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\" width=\"859\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"217\"><span style=\"font-size: 12pt\">\u7c7b\u540d<\/span>\n<\/td>\n<td valign=\"top\" width=\"57\"><span style=\"font-size: 12pt\">\u62bd\u8c61\u7c7b<\/span>\n<\/td>\n<td valign=\"top\" width=\"171\"><span style=\"font-size: 12pt\">\u7236\u7c7b<\/span>\n<\/td>\n<td valign=\"top\" width=\"412\"><span style=\"font-size: 12pt\">\u529f\u80fd<\/span>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"217\"><span style=\"font-size: 12pt\">ContentResult<\/span>\n<\/td>\n<td valign=\"top\" width=\"57\">&nbsp;<\/td>\n<td valign=\"top\" width=\"171\">&nbsp;<\/td>\n<td valign=\"top\" width=\"412\"><span style=\"font-size: 12pt\">\u6839\u636e\u5185\u5bb9\u7684\u7c7b\u578b\u548c\u7f16\u7801,\u6570\u636e\u5185\u5bb9.<\/span>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"217\"><span style=\"font-size: 12pt\">EmptyResult<\/span>\n<\/td>\n<td valign=\"top\" width=\"57\">&nbsp;<\/td>\n<td valign=\"top\" width=\"171\">&nbsp;<\/td>\n<td valign=\"top\" width=\"412\"><span style=\"font-size: 12pt\">\u7a7a\u65b9\u6cd5.<\/span>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"217\"><span style=\"font-size: 12pt\">FileResult <\/span>\n<\/td>\n<td valign=\"top\" width=\"57\"><span style=\"font-size: 12pt\">abstract<\/span>\n<\/td>\n<td valign=\"top\" width=\"171\">&nbsp;<\/td>\n<td valign=\"top\" width=\"412\"><span style=\"font-size: 12pt\">\u5199\u5165\u6587\u4ef6\u5185\u5bb9,\u5177\u4f53\u7684\u5199\u5165\u65b9\u5f0f\u5728\u6d3e\u751f\u7c7b\u4e2d.<\/span>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"217\"><span style=\"font-size: 12pt\">FileContentResult<\/span>\n<\/td>\n<td valign=\"top\" width=\"57\">&nbsp;<\/td>\n<td valign=\"top\" width=\"171\"><span style=\"font-size: 12pt\">FileResult <\/span>\n<\/td>\n<td valign=\"top\" width=\"412\"><span style=\"font-size: 12pt\">\u901a\u8fc7 \u6587\u4ef6byte[] \u5199\u5165\u6587\u4ef6.<\/span>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"217\"><span style=\"font-size: 12pt\">FilePathResult<\/span>\n<\/td>\n<td valign=\"top\" width=\"57\">&nbsp;<\/td>\n<td valign=\"top\" width=\"171\"><span style=\"font-size: 12pt\">FileResult <\/span>\n<\/td>\n<td valign=\"top\" width=\"412\"><span style=\"font-size: 12pt\">\u901a\u8fc7 \u6587\u4ef6\u8def\u5f84 \u5199\u5165\u6587\u4ef6.<\/span>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"217\"><span style=\"font-size: 12pt\">FileStreamResult<\/span>\n<\/td>\n<td valign=\"top\" width=\"57\">&nbsp;<\/td>\n<td valign=\"top\" width=\"171\"><span style=\"font-size: 12pt\">FileResult <\/span>\n<\/td>\n<td valign=\"top\" width=\"412\"><span style=\"font-size: 12pt\">\u901a\u8fc7 \u6587\u4ef6Stream \u5199\u5165\u6587\u4ef6.<\/span>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"217\"><span style=\"font-size: 12pt\">HttpUnauthorizedResult<\/span>\n<\/td>\n<td valign=\"top\" width=\"57\">&nbsp;<\/td>\n<td valign=\"top\" width=\"171\">&nbsp;<\/td>\n<td valign=\"top\" width=\"412\"><span style=\"font-size: 12pt\">\u629b\u51fa401\u9519\u8bef<\/span>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"217\"><span style=\"font-size: 12pt\">JavaScriptResult <\/span>\n<\/td>\n<td valign=\"top\" width=\"57\">&nbsp;<\/td>\n<td valign=\"top\" width=\"171\">&nbsp;<\/td>\n<td valign=\"top\" width=\"412\"><span style=\"font-size: 12pt\">\u8fd4\u56dejavascript\u6587\u4ef6<\/span>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"217\"><span style=\"font-size: 12pt\">JsonResult <\/span>\n<\/td>\n<td valign=\"top\" width=\"57\">&nbsp;<\/td>\n<td valign=\"top\" width=\"171\">&nbsp;<\/td>\n<td valign=\"top\" width=\"412\"><span style=\"font-size: 12pt\">\u8fd4\u56deJson\u683c\u5f0f\u7684\u6570\u636e<\/span>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"217\"><span style=\"font-size: 12pt\">RedirectResult <\/span>\n<\/td>\n<td valign=\"top\" width=\"57\">&nbsp;<\/td>\n<td valign=\"top\" width=\"171\">&nbsp;<\/td>\n<td valign=\"top\" width=\"412\"><span style=\"font-size: 12pt\">\u4f7f\u7528Response.Redirect\u91cd\u5b9a\u5411\u9875\u9762<\/span>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"217\"><span style=\"font-size: 12pt\">RedirectToRouteResult <\/span>\n<\/td>\n<td valign=\"top\" width=\"57\">&nbsp;<\/td>\n<td valign=\"top\" width=\"171\">&nbsp;<\/td>\n<td valign=\"top\" width=\"412\"><span style=\"font-size: 12pt\">\u6839\u636eRoute\u89c4\u5219\u91cd\u5b9a\u5411\u9875\u9762<\/span>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"217\"><span style=\"font-size: 12pt\">ViewResultBase <\/span>\n<\/td>\n<td valign=\"top\" width=\"57\"><span style=\"font-size: 12pt\">abstract<\/span>\n<\/td>\n<td valign=\"top\" width=\"171\">&nbsp;<\/td>\n<td valign=\"top\" width=\"412\"><span style=\"font-size: 12pt\">\u8c03\u7528IView.Render()<\/span>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"217\"><span style=\"font-size: 12pt\">PartialViewResult <\/span>\n<\/td>\n<td valign=\"top\" width=\"57\">&nbsp;<\/td>\n<td valign=\"top\" width=\"171\"><span style=\"font-size: 12pt\">ViewResultBase <\/span>\n<\/td>\n<td valign=\"top\" width=\"412\"><span style=\"font-size: 12pt\">\u8c03\u7528\u7236\u7c7bViewResultBase \u7684ExecuteResult\u65b9\u6cd5. <\/span><br \/>\n<br \/><span style=\"font-size: 12pt\">\u91cd\u5199\u4e86\u7236\u7c7b\u7684FindView\u65b9\u6cd5. <\/span><br \/>\n<br \/><span style=\"font-size: 12pt\">\u5bfb\u627e\u7528\u6237\u63a7\u4ef6.ascx\u6587\u4ef6<\/span>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"217\"><span style=\"font-size: 12pt\">ViewResult <\/span>\n<\/td>\n<td valign=\"top\" width=\"57\">&nbsp;<\/td>\n<td valign=\"top\" width=\"171\"><span style=\"font-size: 12pt\">ViewResultBase <\/span>\n<\/td>\n<td valign=\"top\" width=\"412\"><span style=\"font-size: 12pt\">\u8c03\u7528\u7236\u7c7bViewResultBase \u7684ExecuteResult\u65b9\u6cd5. <\/span><br \/>\n<br \/><span style=\"font-size: 12pt\">\u91cd\u5199\u4e86\u7236\u7c7b\u7684FindView\u65b9\u6cd5. <\/span><br \/>\n<br \/><span style=\"font-size: 12pt\">\u5bfb\u627e\u9875\u9762.aspx\u6587\u4ef6<\/span>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"font-size: 12pt\">\u76ee\u524dASP.NET MVC\u8fd8\u6ca1\u6709\u63d0\u4f9b\u5b98\u65b9\u7684ActionResult\u5217\u8868.\u4e0a\u9762\u7684\u5217\u8868\u662f\u6211\u5728\u6e90\u4ee3\u7801\u4e2d\u5206\u6790\u5f97\u51fa\u7684.\u6709\u4e9b\u89e3\u91ca\u7684\u53ef\u80fd\u4e0d\u591f\u6e05\u695a,\u8bf7\u8c05\u89e3.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u4e0b\u9762\u6211\u5c06\u5217\u4e3e\u5404\u4e2aActionResult\u7684\u5b9e\u4f8b.<\/span>\n<\/p>\n<h2><span style=\"font-size: 12pt\">\u4e94.\u5b9e\u4f8b\u5e94\u7528<\/span><br \/>\n<\/h2>\n<h3><span style=\"font-size: 12pt\">1.\u6dfb\u52a0Controller<\/span><br \/>\n<\/h3>\n<p><span style=\"font-size: 12pt\">\u5b89\u88c5\u4e86ASP.NET MVC\u540e, \u5728\u9879\u76ee\u4e0a\u70b9\u51fb\u53f3\u952e\u4f1a\u627e\u5230\u6dfb\u52a0Controller\u9879:<\/span>\n<\/p>\n<p><a href=\"http:\/\/images.cnblogs.com\/cnblogs_com\/zhangziqiu\/WindowsLiveWriter\/3903880e4390_7859\/image_6.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" alt=\"image\" border=\"0\" height=\"255\" src=\"http:\/\/images.cnblogs.com\/cnblogs_com\/zhangziqiu\/WindowsLiveWriter\/3903880e4390_7859\/image_thumb_2.png\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" title=\"image\" width=\"469\" \/><\/a>\n<\/p>\n<h3><span style=\"font-size: 12pt\">2.\u6dfb\u52a0Action<\/span><br \/>\n<\/h3>\n<p><span style=\"font-size: 12pt\">\u4e0b\u9762\u8fd9\u4e2a\u7c7b\u63d0\u4f9b\u4e86\u8fd4\u56de\u5404\u79cd\u7c7b\u578b\u7684ActionResult\u7684Action\u5b9e\u4f8b:<\/span>\n<\/p>\n<pre class=\"csharpcode\"><span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n <span class=\"kwrd\" style=\"font-size: 12pt\">class<\/span>\r\n<span style=\"font-size: 12pt\"> DemoController : Controller\r\n    {\r\n\r\n        <\/span>\r\n<span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ http:\/\/localhost:1847\/Demo\/ContentResultDemo<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;\/summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;returns&gt;&lt;\/returns&gt;<\/span>\r\n\r\n        <span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n<span style=\"font-size: 12pt\"> ActionResult ContentResultDemo()\r\n        {\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">string<\/span>\r\n<span style=\"font-size: 12pt\"> contentString = <\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;ContextResultDemo!&quot;<\/span>\r\n<span style=\"font-size: 12pt\">;\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n<span style=\"font-size: 12pt\"> Content(contentString);\r\n        }\r\n\r\n        <\/span>\r\n<span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ http:\/\/localhost:1847\/Demo\/EmptyResultDemo<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;\/summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;returns&gt;&lt;\/returns&gt;<\/span>\r\n\r\n        <span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n<span style=\"font-size: 12pt\"> ActionResult EmptyResultDemo()\r\n        {\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n  <span class=\"kwrd\" style=\"font-size: 12pt\">new<\/span>\r\n<span style=\"font-size: 12pt\"> EmptyResult();\r\n        }\r\n\r\n        <\/span>\r\n<span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ http:\/\/localhost:1847\/Demo\/FileContentResultDemo<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;\/summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;returns&gt;&lt;\/returns&gt;<\/span>\r\n\r\n        <span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n<span style=\"font-size: 12pt\"> ActionResult FileContentResultDemo()\r\n        {\r\n            FileStream fs = <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">new<\/span>\r\n<span style=\"font-size: 12pt\"> FileStream(Server.MapPath(<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">@&quot;\/resource\/Images\/1.gif&quot;<\/span>\r\n<span style=\"font-size: 12pt\">), FileMode.Open, FileAccess.Read);\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">byte<\/span>\r\n<span style=\"font-size: 12pt\">[] buffer = <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">new<\/span>\r\n <span class=\"kwrd\" style=\"font-size: 12pt\">byte<\/span>\r\n<span style=\"font-size: 12pt\">[Convert.ToInt32(fs.Length)];\r\n            fs.Read(buffer, 0, Convert.ToInt32(fs.Length) );\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n<span style=\"font-size: 12pt\"> File(buffer, <\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">@&quot;image\/gif&quot;<\/span>\r\n<span style=\"font-size: 12pt\">);\r\n        }\r\n\r\n        <\/span>\r\n<span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ http:\/\/localhost:1847\/Demo\/FilePathResultDemo<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;\/summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;returns&gt;&lt;\/returns&gt;<\/span>\r\n\r\n        <span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n<span style=\"font-size: 12pt\"> ActionResult FilePathResultDemo()\r\n        {\r\n            <\/span>\r\n<span class=\"rem\" style=\"font-size: 12pt\">\/\/\u53ef\u4ee5\u5c06\u4e00\u4e2ajpg\u683c\u5f0f\u7684\u56fe\u50cf\u8f93\u51fa\u4e3agif\u683c\u5f0f<\/span>\r\n\r\n            <span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n<span style=\"font-size: 12pt\"> File(Server.MapPath(<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">@&quot;\/resource\/Images\/2.jpg&quot;<\/span>\r\n<span style=\"font-size: 12pt\">), <\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">@&quot;image\/gif&quot;<\/span>\r\n<span style=\"font-size: 12pt\">);\r\n        }\r\n\r\n        <\/span>\r\n<span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ http:\/\/localhost:1847\/Demo\/FileStreamResultDemo<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;\/summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;returns&gt;&lt;\/returns&gt;<\/span>\r\n\r\n        <span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n<span style=\"font-size: 12pt\"> ActionResult FileStreamResultDemo()\r\n        {            \r\n            FileStream fs = <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">new<\/span>\r\n<span style=\"font-size: 12pt\"> FileStream(Server.MapPath(<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">@&quot;\/resource\/Images\/1.gif&quot;<\/span>\r\n<span style=\"font-size: 12pt\">), FileMode.Open, FileAccess.Read);\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n<span style=\"font-size: 12pt\"> File(fs, <\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">@&quot;image\/gif&quot;<\/span>\r\n<span style=\"font-size: 12pt\">);\r\n        }\r\n\r\n        <\/span>\r\n<span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ http:\/\/localhost:1847\/Demo\/HttpUnauthorizedResultDemo<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;\/summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;returns&gt;&lt;\/returns&gt;<\/span>\r\n\r\n        <span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n<span style=\"font-size: 12pt\"> ActionResult HttpUnauthorizedResultDemo()\r\n        {\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n <span class=\"kwrd\" style=\"font-size: 12pt\">new<\/span>\r\n<span style=\"font-size: 12pt\"> HttpUnauthorizedResult();\r\n        }\r\n\r\n        <\/span>\r\n<span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ http:\/\/localhost:1847\/Demo\/JavaScriptResultDemo<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;\/summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;returns&gt;&lt;\/returns&gt;<\/span>\r\n\r\n        <span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n<span style=\"font-size: 12pt\"> ActionResult JavaScriptResultDemo()\r\n        {\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n<span style=\"font-size: 12pt\"> JavaScript(<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">@&quot;alert(&quot;<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;Test JavaScriptResultDemo!&quot;<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;)&quot;<\/span>\r\n<span style=\"font-size: 12pt\">);\r\n        }\r\n\r\n        <\/span>\r\n<span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ http:\/\/localhost:1847\/Demo\/JsonResultDemo<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;\/summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;returns&gt;&lt;\/returns&gt;<\/span>\r\n\r\n        <span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n<span style=\"font-size: 12pt\"> ActionResult JsonResultDemo()\r\n        {\r\n            var tempObj = <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">new<\/span>\r\n<span style=\"font-size: 12pt\"> { Controller = <\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;DemoController&quot;<\/span>\r\n<span style=\"font-size: 12pt\">, Action = <\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;JsonResultDemo&quot;<\/span>\r\n<span style=\"font-size: 12pt\"> };\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n<span style=\"font-size: 12pt\"> Json(tempObj);\r\n        }\r\n\r\n        <\/span>\r\n<span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ http:\/\/localhost:1847\/Demo\/RedirectResultDemo<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;\/summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;returns&gt;&lt;\/returns&gt;<\/span>\r\n\r\n        <span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n<span style=\"font-size: 12pt\"> ActionResult RedirectResultDemo()\r\n        {\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n<span style=\"font-size: 12pt\"> Redirect(<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">@&quot;http:\/\/localhost:1847\/Demo\/ContentResultDemo&quot;<\/span>\r\n<span style=\"font-size: 12pt\">);\r\n        }\r\n\r\n        <\/span>\r\n<span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ http:\/\/localhost:1847\/Demo\/RedirectToRouteResultDemo<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;\/summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;returns&gt;&lt;\/returns&gt;<\/span>\r\n\r\n        <span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n<span style=\"font-size: 12pt\"> ActionResult RedirectToRouteResultDemo()\r\n        {\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n<span style=\"font-size: 12pt\"> RedirectToAction(<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">@&quot;FileStreamResultDemo&quot;<\/span>\r\n<span style=\"font-size: 12pt\">);\r\n        }\r\n\r\n        <\/span>\r\n<span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ http:\/\/localhost:1847\/Demo\/PartialViewResultDemo<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;\/summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;returns&gt;&lt;\/returns&gt;<\/span>\r\n\r\n        <span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n<span style=\"font-size: 12pt\"> ActionResult PartialViewResultDemo()\r\n        {\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n<span style=\"font-size: 12pt\"> PartialView();\r\n        }\r\n\r\n        <\/span>\r\n<span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ http:\/\/localhost:1847\/Demo\/RedirectToRouteResultDemo<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;\/summary&gt;<\/span>\r\n\r\n        <span class=\"rem\" style=\"font-size: 12pt\">\/\/\/ &lt;returns&gt;&lt;\/returns&gt;<\/span>\r\n\r\n        <span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n<span style=\"font-size: 12pt\"> ActionResult ViewResultDemo()\r\n        {\r\n            <\/span>\r\n<span class=\"rem\" style=\"font-size: 12pt\">\/\/\u5982\u679c\u6ca1\u6709\u4f20\u5165View\u540d\u79f0, \u9ed8\u8ba4\u5bfb\u627e\u4e0eAction\u540d\u79f0\u76f8\u540c\u7684View\u9875\u9762.<\/span>\r\n\r\n            <span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n<span style=\"font-size: 12pt\"> View();\r\n        }\r\n\r\n    }<\/span>\r\n<\/pre>\n<style type=\"text\/css\">\n<p>.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u5728\u6587\u7ae0\u6700\u540e\u63d0\u4f9b\u6709\u5b8c\u6574\u5b9e\u4f8b\u4ee3\u7801\u4e0b\u8f7d.<\/span>\n<\/p>\n<h2><span style=\"font-size: 12pt\">\u516d.Controller \u6df1\u5165\u5206\u6790<\/span><br \/>\n<\/h2>\n<p><span style=\"font-size: 12pt\">\u5728\u7814\u7a76Controller\/Action\u7684\u6d41\u7a0b\u8fc7\u7a0b\u4e2d, \u53d1\u73b0\u4e86ASP.NET MVC\u4e00\u4e9b\u95ee\u9898.<\/span>\n<\/p>\n<h3><span style=\"font-size: 12pt\">1.Routing\u7ec4\u4ef6\u4e0eMVC\u6846\u67b6\u7684\u7ed3\u5408<\/span><br \/>\n<\/h3>\n<p><span style=\"font-size: 12pt\">Routing\u7ec4\u4ef6\u548cASP.NET MVC\u5e76\u4e0d\u662f\u4e00\u4e2a\u9879\u76ee, \u5728ASP.NET MVC\u4e2d\u4ec5\u4ec5\u662f\u4f7f\u7528\u4e86Routing\u7ec4\u4ef6, \u5728\u6e90\u4ee3\u7801\u4e2d\u662f\u901a\u8fc7dll\u7684\u65b9\u5f0f\u5f15\u7528\u7684.Routing\u7ec4\u4ef6\u5df2\u7ecf\u5305\u542b\u5728.net framework 3.5 sp1\u4e2d\u4e86.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u90a3\u4e48ASP.NET MVC\u662f\u5982\u4f55\u5e94\u7528Routing\u7ec4\u4ef6\u7684\u5462?<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">Routing\u7ec4\u4ef6\u83b7\u53d6\u4e86Url\u4e2d\u7684\u6570\u636e\u540e, \u4f1a\u5c06\u6570\u636e\u4fdd\u5b58\u5728\u4e00\u4e2a RouteData \u5bf9\u8c61\u4e2d.\u5e76\u5c06\u8bf7\u6c42\u4f20\u9012\u7ed9\u4e00\u4e2a\u5b9e\u73b0\u4e86IRouteHandler\u63a5\u53e3\u7684\u5bf9\u8c61. \u5728Asp.net MVC\u4e2d\u63d0\u4f9b\u7684MvcRouteHandler\u7c7b\u5b9e\u73b0\u4e86\u6b64\u63a5\u53e3, Routing \u5c06\u8bf7\u6c42\u4f20\u9012\u7ed9MvcRouteHandler\u7684GetHttpHandler\u65b9\u6cd5.\u4e0b\u9762\u662f\u6e90\u4ee3\u7801:<\/span>\n<\/p>\n<p><strong style=\"font-size: 12pt\">IRouteHandler\u63a5\u53e3:<\/strong>\n<\/p>\n<pre class=\"csharpcode\">    <span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n <span class=\"kwrd\" style=\"font-size: 12pt\">interface<\/span>\r\n<span style=\"font-size: 12pt\"> IRouteHandler\r\n    {\r\n        IHttpHandler GetHttpHandler(RequestContext requestContext);\r\n    }<\/span>\r\n<\/pre>\n<style type=\"text\/css\">\n<p>.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<\/p>\n<p>&nbsp;<\/p>\n<p><strong style=\"font-size: 12pt\">MvcRouteHandler\u7c7b:<\/strong>\n<\/p>\n<pre class=\"csharpcode\">    <span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n <span class=\"kwrd\" style=\"font-size: 12pt\">class<\/span>\r\n<span style=\"font-size: 12pt\"> MvcRouteHandler : IRouteHandler {\r\n        <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">protected<\/span>\r\n <span class=\"kwrd\" style=\"font-size: 12pt\">virtual<\/span>\r\n<span style=\"font-size: 12pt\"> IHttpHandler GetHttpHandler(RequestContext requestContext) {\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n <span class=\"kwrd\" style=\"font-size: 12pt\">new<\/span>\r\n<span style=\"font-size: 12pt\"> MvcHandler(requestContext);\r\n        }\r\n\r\n        <\/span>\r\n<span class=\"preproc\" style=\"font-size: 12pt\">#region<\/span>\r\n<span style=\"font-size: 12pt\"> IRouteHandler Members\r\n        IHttpHandler IRouteHandler.GetHttpHandler(RequestContext requestContext) {\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n<span style=\"font-size: 12pt\"> GetHttpHandler(requestContext);\r\n        }\r\n        <\/span>\r\n<span class=\"preproc\" style=\"font-size: 12pt\">#endregion<\/span>\r\n<span style=\"font-size: 12pt\">\r\n    }<\/span>\r\n<\/pre>\n<style type=\"text\/css\">\n<p>.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 12pt\">\u66fe\u7ecf\u6211\u8ba4\u4e3aIRouteHandler\u662f\u591a\u4f59\u7684, \u7528IHttpHandler\u5c31\u591f\u4e86. \u73b0\u5728\u77e5\u9053\u4e86\u4e3a\u4f55\u8981\u5b9a\u4e49\u8fd9\u4e2a\u63a5\u53e3. \u4e3b\u8981\u662f\u4e3a\u4e86\u4f20\u9012RouteData\u5bf9\u8c61.GetHttpHandler\u65b9\u6cd5\u9700\u8981\u4e00\u4e2aRequestContext \u5bf9\u8c61.RequestContext \u662f System.Web.Routing\u7a0b\u5e8f\u96c6\u4e2d\u7684\u7c7b, \u91cc\u9762\u9664\u4e86\u5904\u7406\u8bf7\u6c42\u9700\u8981\u7684HttpContextBase\u5bf9\u8c61,\u8fd8\u5305\u62ec\u4e86\u4e00\u4e2aRouteData\u5bf9\u8c61.<\/span>\n<\/p>\n<p><strong style=\"font-size: 12pt\">RequestContext\u7c7b:<\/strong>\n<\/p>\n<pre class=\"csharpcode\">    <span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n <span class=\"kwrd\" style=\"font-size: 12pt\">class<\/span>\r\n<span style=\"font-size: 12pt\"> RequestContext\r\n    {\r\n        <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n<span style=\"font-size: 12pt\"> RequestContext(HttpContextBase httpContext, RouteData routeData);\r\n        <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n<span style=\"font-size: 12pt\"> HttpContextBase HttpContext { get; }\r\n        <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">public<\/span>\r\n<span style=\"font-size: 12pt\"> RouteData RouteData { get; }\r\n    }<\/span>\r\n<\/pre>\n<p><span style=\"font-size: 12pt\">Routing\u7ec4\u4ef6\u5728Web.Config\u4e2d\u6ce8\u518c\u4e86\u4e00\u4e2aHttpModule: System.Web.Routing.UrlRoutingModule, \u800c\u4e0d\u662fHttpHandler:<\/span>\n<\/p>\n<pre class=\"csharpcode\"><span class=\"kwrd\" style=\"font-size: 12pt\">&lt;<\/span>\r\n<span class=\"html\" style=\"font-size: 12pt\">add<\/span>\r\n <span class=\"attr\" style=\"font-size: 12pt\">name<\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">=&quot;UrlRoutingModule&quot;<\/span>\r\n <span class=\"attr\" style=\"font-size: 12pt\">type<\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">=&quot;System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;<\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">\/&gt;<\/span>\r\n<\/pre>\n<style type=\"text\/css\">\n<p>.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 12pt\">\u53ef\u60dc\u770b\u4e0d\u5230\u8fd9\u4e2a\u7c7b\u7684\u6e90\u4ee3\u7801. \u6240\u6709\u8bf7\u6c42\u6700\u540e\u90fd\u662f\u8981\u4f20\u9012\u7ed9IHttpHandler\u5bf9\u8c61\u5904\u7406, \u4e3b\u8981\u7684\u5de5\u4f5c\u662f\u7f16\u8bd1\u9875\u9762, \u6240\u4ee5\u6211\u731c\u6d4b\u8fd9\u4e2aModule\u5c06\u8bf7\u6c42\u622a\u83b7\u540e\u901a\u8fc7IRouteHandler\u63a5\u53e3\u5bf9\u8c61\u83b7\u53d6\u4e00\u4e2aHttpHandler, \u7136\u540e\u5c06\u5904\u7406\u79fb\u4ea4\u7ed9\u83b7\u53d6\u5230\u7684HttpHandler.<\/span>\n<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"font-size: 12pt\">ASP.NET MVC \u4e2d\u5b9e\u73b0\u4e86IHttpHandler\u63a5\u53e3\u7684\u7c7b\u662fMvcHandler, MvcRouteHandler.GetHttpHandler\u65b9\u6cd5\u5c31\u662f\u8fd4\u56de\u4e00\u4e2aMvcHandler\u5bf9\u8c61. MvcHandler\u7c7b\u7684\u6784\u9020\u51fd\u6570\u9700\u8981\u4f20\u5165\u4e00\u4e2aRequestContext\u5bf9\u8c61. \u5b9e\u73b0\u7684IHttpHandler\u63a5\u53e3\u65b9\u6cd5\u5904\u7406\u8fc7\u7a0b\u4e2d\u90fd\u9700\u8981\u4f9d\u8d56\u8fd9\u4e2a\u5bf9\u8c61.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u4f46\u662f\u5fae\u8f6f\u5728\u8fd9\u91cc\u7684\u5904\u7406\u6709\u4e00\u4e9b\u4e0d\u8db3. MvcHandler\u867d\u7136\u5b9e\u73b0\u4e86IHttpHandler\u63a5\u53e3\u4f46\u662f\u4e0d\u80fd\u88ab\u5f53\u4f5cIHttpHandler\u63a5\u53e3\u4f7f\u7528. \u56e0\u4e3aIHttpHandler\u4e2d\u6ca1\u6709\u5b9a\u4e49RequestContext\u5c5e\u6027, \u5982\u679c\u4e00\u4e2aMvcHandler\u5bf9\u8c61\u6b64\u5c5e\u6027\u6ca1\u6709\u8d4b\u503c\u5219\u4f1a\u51fa\u9519, \u4e5f\u6ca1\u6709\u5c06\u9ed8\u8ba4\u7684\u65e0\u53c2\u6570\u6784\u9020\u51fd\u6570\u8bbe\u7f6e\u4e3aprivate, \u6240\u4ee5\u7406\u8bba\u4e0a\u53ef\u4ee5\u5f88\u968f\u610f\u7684\u5b9e\u4f8b\u5316\u4e00\u4e2aMvcHandler\u800c\u4e0d\u4e3a\u5176RequestContext\u5c5e\u6027\u8d4b\u503c.<\/span>\n<\/p>\n<p><strong style=\"font-size: 12pt\">IRouteHandler\u60f3\u5b9e\u73b0\u7684\u8bed\u610f\u662f: \u8fd4\u56de\u4e00\u4e2a\u5177\u6709RequestContext\u5c5e\u6027\u7684IHttpHandler\u5bf9\u8c61.<\/strong>\n<\/p>\n<p><strong style=\"font-size: 12pt\">\u4f46\u662f\u6700\u540e\u7684\u5b9e\u73b0\u7ed3\u679c\u662f: \u63d0\u4f9b&quot;\u8fd4\u56deIHttpHandler\u5bf9\u8c61&quot;\u7684\u65b9\u6cd5,&nbsp; \u6b64\u65b9\u6cd5\u63a5\u6536RequestContext\u5bf9\u8c61\u53c2\u6570.<\/strong>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u8fd8\u9700\u8981\u6ce8\u610fControllerContext\u7c7b. \u5728Controller\u7684\u5904\u7406\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u6b64\u5bf9\u8c61\u4f5c\u4e3a\u4fdd\u5b58\u4e0a\u4e0b\u6587\u6570\u636e\u7684\u5bb9\u5668.\u4e0b\u9762\u662f\u8fd9\u51e0\u4e2a\u7c7b\u7684\u5305\u542b\u5173\u7cfb:<\/span>\n<\/p>\n<p><a href=\"http:\/\/images.cnblogs.com\/cnblogs_com\/zhangziqiu\/WindowsLiveWriter\/3903880e4390_7859\/image_4.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" alt=\"image\" border=\"0\" height=\"304\" src=\"http:\/\/images.cnblogs.com\/cnblogs_com\/zhangziqiu\/WindowsLiveWriter\/3903880e4390_7859\/image_thumb_1.png\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" title=\"image\" width=\"404\" \/><\/a>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u53ef\u4ee5\u770b\u5230\u5728ControllerContext\u4e2d\u5305\u542b\u4e86RequestContext\u5bf9\u8c61,\u4f46\u662f\u53c8\u5c06RequestContext\u5bf9\u8c61\u4e2d\u7684\u4e24\u4e2a\u5c5e\u6027\u63d0\u53d6\u5230\u81ea\u5df1\u7684\u7c7b\u4e2d.\u5982\u679c\u4ec5\u4ec5\u662f\u4e3a\u4e86\u4f7f\u7528\u65b9\u4fbf\u800c\u8fd9\u4e48\u505a, \u4e2a\u4eba\u8ba4\u4e3a\u4e0d\u662f\u4e00\u4e2a\u597d\u7684\u8bbe\u8ba1.\u6570\u636e\u5bf9\u8c61\u7684\u5b58\u50a8\u804c\u8d23\u4e5f\u5e94\u8be5\u660e\u786e,\u4f7f\u7528ControllerContext.RequestContext.RouteData \u7684\u65b9\u5f0f\u66f4\u5bb9\u6613\u88ab\u4eba\u7406\u89e3.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">PS:\u8fd9\u79cd\u65b9\u5f0f\u7c7b\u4f3c\u4e8e\u65b9\u6cd5\u5185\u8054.\u5bf9\u4e8e\u5c5e\u6027JIT\u4e3a\u4e86\u6548\u7387\u4f1a\u5e2e\u52a9\u6211\u4eec\u505a\u5185\u8054.\u800c\u4ec5\u4ec5\u662f\u4e3a\u4e86\u4f7f\u7528\u65b9\u4fbf.<\/span>\n<\/p>\n<h3><span style=\"font-size: 12pt\">2.IView \u4e0e View\u5bf9\u8c61\u7684\u5173\u7cfb<\/span><br \/>\n<\/h3>\n<p><strong style=\"font-size: 12pt\">\u6240\u4ee5\u4ece\u7cfb\u7edf\u7684\u89d2\u5ea6\u4e0a\u770b, \u5b9e\u73b0\u4e86IView\u63a5\u53e3\u7684\u5bf9\u8c61\u624d\u662fView.<\/strong>\n<\/p>\n<p><strong style=\"font-size: 12pt\">\u4f46\u662f\u4ece\u5b9e\u73b0\u6548\u679c\u4e0a\u770b, \u5177\u4f53\u7684aspx\u6216\u8005ascx\u9875\u9762\u624d\u662fView.<\/strong>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u5f53\u7b2c\u4e00\u6b21\u770b\u5230IView\u63a5\u53e3\u65f6\u6211\u8ba4\u4e3a\u5b83\u5e94\u8be5\u662f&quot;View\u89d2\u8272&quot;\u9700\u8981\u5b9e\u73b0\u7684\u63a5\u53e3. \u4f46\u662f\u7ed3\u679c\u5e76\u4e0d\u662f\u8fd9\u6837. <\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u5728\u6211\u4eec\u7684\u7cfb\u7edf\u4e2dView\u5bf9\u8c61\u5e94\u8be5\u662faspx\u6216\u8005ascx\u6587\u4ef6. \u800c\u4e14\u5e76\u4e0d\u662f\u6240\u6709\u7684ActionResult\u90fd\u9700\u8981\u627e\u5230aspx\u6216\u8005ascx\u6587\u4ef6, \u4e8b\u5b9e\u4e0a\u53ea\u6709PartialViewResult \u548c ViewResult \u624d\u4f1a\u53bb\u5bfb\u627eView\u5bf9\u8c61.\u5176\u4ed6\u7684ActionResult\u8981\u4e48\u662f\u8fd4\u56de\u6587\u4ef6, \u8981\u4e48\u662f\u8df3\u8f6c\u7b49\u7b49.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u90a3\u4e48\u4e24\u8005\u7684\u5173\u7cfb\u5230\u5e95\u662f\u600e\u6837\u7684? \u5176\u5b9e\u5176\u4e2d\u7684\u8fc7\u7a0b\u9700\u8981\u7275\u626f\u5230\u8fd9\u51e0\u4e2a\u63a5\u53e3\u548c\u7c7b:<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">IViewEngine, ViewEngineResult, ViewEngineCollection<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">ViewEngine\u662fView\u5f15\u64ce, ViewEngineCollection\u662f\u4e00\u4e2a\u5f15\u64ce\u96c6\u5408,\u91cc\u9762\u4fdd\u5b58\u4e86\u5404\u79cd\u5bfb\u627eView\u7684\u5f15\u64ce.\u4f46\u662f\u5728\u76ee\u524d\u7684\u6e90\u4ee3\u7801\u4e2d\u53ea\u6709WebFormViewEngine : VirtualPathProviderViewEngine : IViewEngine<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u8fd9\u4e00\u7cfb\u5217WebForm\u4f7f\u7528\u7684\u5f15\u64ce.\u5f15\u64ce\u7684\u4f5c\u7528\u6709\u4e24\u4e2a:<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">1.\u5bfb\u627ePage\/\u7528\u6237\u63a7\u4ef6\u7684\u8def\u5f84<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">2.\u6839\u636e\u8def\u5f84\u521b\u5efaIView\u5bf9\u8c61.\u4e5f\u5c31\u662f\u6839\u636e\u9875\u9762\u7684\u7269\u7406\u6587\u4ef6\u521b\u5efaIView\u63a5\u53e3\u5bf9\u8c61.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u800c\u4e14\u76ee\u524d\u5b9e\u73b0\u4e86IView\u63a5\u53e3\u7684\u5bf9\u8c61\u4e5f\u53ea\u6709\u4e00\u4e2a:<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">WebFormView<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">WebFormViewEngine \u6839\u636e\u9875\u9762\u8def\u5f84, \u5c06\u4e00\u4e2a\u9875\u9762\u5730\u5740\u8f6c\u5316\u4e3a\u4e00\u4e2aWebFormView\u5bf9\u8c61,\u4e5f\u5c31\u662f\u4e00\u4e2aIView\u63a5\u53e3\u5bf9\u8c61.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u81f3\u6b64IView\u63a5\u53e3\u548cPage\u9875\u9762\u7c7b\u4ecd\u7136\u6ca1\u6709\u4efb\u4f55\u5173\u7cfb, IView\u5bf9\u8c61\u53ea\u662f\u4fdd\u5b58\u4e86\u9875\u9762\u7684\u7269\u7406\u8def\u5f84.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u63a5\u7740\u5728IView\u7684Render\u4e8b\u4ef6\u4e2d,\u6839\u636e\u7269\u7406\u8def\u5f84\u521b\u5efa\u4e86\u4e00\u4e2a\u9875\u9762\u7684object\u5b9e\u4f8b,\u6ce8\u610f\u770b\u8fd9\u4e00\u6bb5\u4ee3\u7801:<\/span>\n<\/p>\n<pre class=\"csharpcode\">            <span class=\"kwrd\" style=\"font-size: 12pt\">object<\/span>\r\n<span style=\"font-size: 12pt\"> viewInstance = BuildManager.CreateInstanceFromVirtualPath(ViewPath, <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">typeof<\/span>\r\n<span style=\"font-size: 12pt\">(<\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">object<\/span>\r\n<span style=\"font-size: 12pt\">));\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">if<\/span>\r\n<span style=\"font-size: 12pt\"> (viewInstance == <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">null<\/span>\r\n<span style=\"font-size: 12pt\">) {\r\n                <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">throw<\/span>\r\n <span class=\"kwrd\" style=\"font-size: 12pt\">new<\/span>\r\n<span style=\"font-size: 12pt\"> InvalidOperationException(\r\n                    String.Format(\r\n                        CultureInfo.CurrentUICulture,\r\n                        MvcResources.WebFormViewEngine_ViewCouldNotBeCreated,\r\n                        ViewPath));\r\n            }\r\n\r\n            ViewPage viewPage = viewInstance <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">as<\/span>\r\n<span style=\"font-size: 12pt\"> ViewPage;\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">if<\/span>\r\n<span style=\"font-size: 12pt\"> (viewPage != <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">null<\/span>\r\n<span style=\"font-size: 12pt\">) {\r\n                RenderViewPage(viewContext, viewPage);\r\n                <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n<span style=\"font-size: 12pt\">;\r\n            }\r\n\r\n            ViewUserControl viewUserControl = viewInstance <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">as<\/span>\r\n<span style=\"font-size: 12pt\"> ViewUserControl;\r\n            <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">if<\/span>\r\n<span style=\"font-size: 12pt\"> (viewUserControl != <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">null<\/span>\r\n<span style=\"font-size: 12pt\">) {\r\n                RenderViewUserControl(viewContext, viewUserControl);\r\n                <\/span>\r\n<span class=\"kwrd\" style=\"font-size: 12pt\">return<\/span>\r\n<span style=\"font-size: 12pt\">;\r\n            }<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<style type=\"text\/css\">\n<p>.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<\/p>\n<p><span style=\"font-size: 12pt\">viewInstance \u5c31\u662f\u901a\u8fc7\u7269\u7406\u8def\u5f84\u521b\u5efa\u7684\u9875\u9762\u5bf9\u8c61.\u4f46\u662f\u4ed6\u7684\u7c7b\u578b\u662fobject, \u800c\u4e14\u7a0b\u5e8f\u5c1d\u8bd5\u5c06\u5176\u5206\u522b\u8f6c\u5316\u4e3aViewPage\u5bf9\u8c61\u548cViewUserControl\u5bf9\u8c61.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u6211\u60f3\u5f88\u591a\u4eba\u90fd\u770b\u5230\u4e86\u8fd9\u91cc\u7684\u8bbe\u8ba1\u4e0d\u8db3.\u73b0\u5728\u6211\u4eec\u53ea\u80fd&quot;\u7ea6\u5b9a&quot;: \u6240\u6709\u7684MVC\u4e2d\u7684\u9875\u9762\u5bf9\u8c61\u90fd\u5fc5\u987b\u7ee7\u627f\u81eaViewPage\u6216\u8005ViewUserControl\u7c7b, \u5426\u5219\u7a0b\u5e8f\u5c31\u4f1a\u51fa\u9519.\u4ea7\u751f\u8fd9\u79cd\u4e0d\u8db3\u7684\u539f\u56e0\u5c31\u662fIView\u63a5\u53e3\u548cViewPage\u6ca1\u6709\u4efb\u4f55\u7684\u8026\u5408\u6027, \u5b8c\u5168\u662f\u786c\u7f16\u7801\u8fdb\u53bb\u7684.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u4e3a\u4ec0\u4e48\u4e0d\u8ba9\u9875\u9762\u76f4\u63a5\u5b9e\u73b0IView\u63a5\u53e3? \u7136\u540e\u5c1d\u8bd5\u5c06\u9875\u9762\u8f6c\u5316\u4e3aIView\u63a5\u53e3\u5bf9\u8c61, \u800c\u4e0d\u662fViewPage, \u8fd9\u6837\u624d\u662f\u597d\u7684\u8bbe\u8ba1. \u5176\u5b9e\u5fae\u8f6f\u77e5\u9053\u4ec0\u4e48\u662f\u597d\u7684\u8bbe\u8ba1, \u6211\u731c\u6d4b\u4ed6\u4eec\u9047\u5230\u7684\u56f0\u96be\u662fPage\u5bf9\u8c61\u548cIView\u63a5\u53e3\u7684\u51b2\u7a81. \u56e0\u4e3a\u4e24\u8005\u90fd\u9700\u8981Render. \u5982\u679c\u5728IView\u4e2d\u5b9a\u4e49\u81ea\u5df1\u7684Render\u540d\u79f0, \u90a3\u5c31\u610f\u5473\u7740ASP.NET MVC\u5f00\u53d1\u5c0f\u7ec4\u8981\u81ea\u5df1\u5904\u7406\u9875\u9762\u7684\u663e\u793a\u903b\u8f91, \u800c\u73b0\u5728ASP.NET WebForm\u6a21\u5f0f\u4e0b\u9762\u7684\u9875\u9762\u663e\u793a\u5f15\u64ce\u53c8\u4e0d\u80fd\u590d\u7528, \u91cd\u65b0\u5f00\u53d1\u81ea\u5df1\u7684\u4e00\u5957\u663e\u793a\u5f15\u64ce\u6210\u672c\u53c8\u592a\u5927, \u624d\u51fa\u6b64\u4e0b\u7b56.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u4ee5\u4e0a\u53ea\u662f\u731c\u6d4b.\u8fd9\u79cd\u8bbe\u8ba1\u7684\u7f3a\u9677\u867d\u7136\u53ef\u4ee5\u63a5\u53d7, \u4f46\u662f\u771f\u7684\u662f\u8ba9\u6211\u597d\u51e0\u5929\u9677\u5165\u4e86\u770b\u4e0d\u61c2\u4ee3\u7801\u7684\u75db\u82e6\u4e4b\u4e2d.\u8fd8\u597d, \u73b0\u5728\u53ef\u4ee5\u89e3\u8131\u4e86.<\/span>\n<\/p>\n<p>&nbsp;<\/p>\n<h2><span style=\"font-size: 12pt\">\u4e03.\u5982\u4f55\u5728MVC\u9879\u76ee\u4e2d\u4f7f\u7528MVC\u6e90\u4ee3\u7801\u9879\u76ee<\/span><br \/>\n<\/h2>\n<p><span style=\"font-size: 12pt\">\u53e6\u5916\u5728\u4e3a\u4e86\u8ddf\u8e2a\u5b9e\u73b0\u8fc7\u7a0b, \u6211\u5c06ASP.NET MVC\u7684\u6e90\u4ee3\u7801\u9879\u76ee\u6dfb\u52a0\u5230\u4e86\u5b9e\u4f8b\u9879\u76ee\u4e2d, \u5176\u4e2d\u6709\u4e00\u4e9b\u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9:<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">1. \u5c06\u5b9e\u4f8b\u9879\u76ee\u4e2d\u7684System.Web.Mvc\u5f15\u7528\u5220\u9664, \u6539\u6210\u9879\u76ee\u5f15\u7528.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">2. \u9700\u8981\u5728Web.Config\u4e2d\u6ce8\u91ca\u6389\u7a0b\u5e8f\u96c6\u5f15\u7528:<\/span>\n<\/p>\n<pre class=\"csharpcode\"><span style=\"font-size: 12pt\">        &lt;compilation debug=<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;true&quot;<\/span>\r\n<span style=\"font-size: 12pt\">&gt;\r\n            &lt;assemblies&gt;\r\n                &lt;add assembly=<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089&quot;<\/span>\r\n<span style=\"font-size: 12pt\">\/&gt;\r\n                &lt;add assembly=<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;<\/span>\r\n<span style=\"font-size: 12pt\">\/&gt;\r\n                &lt;add assembly=<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;System.Web.Abstractions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;<\/span>\r\n<span style=\"font-size: 12pt\">\/&gt;\r\n                &lt;add assembly=<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;<\/span>\r\n<span style=\"font-size: 12pt\">\/&gt;\r\n                <\/span>\r\n<strong style=\"font-size: 12pt\">&lt;!-- &lt;add assembly=<span class=\"str\">&quot;System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;<\/span>\r\n\/&gt;--&gt;\r\n<\/strong>\r\n                &lt;add assembly=<span class=\"str\">&quot;System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089&quot;<\/span>\r\n\/&gt;\r\n                &lt;add assembly=<span class=\"str\">&quot;System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089&quot;<\/span>\r\n\/&gt;\r\n                &lt;add assembly=<span class=\"str\">&quot;System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089&quot;<\/span>\r\n\/&gt;\r\n            &lt;\/assemblies&gt;\r\n        &lt;\/compilation&gt;<\/pre>\n<p>&nbsp;<\/p>\n<style type=\"text\/css\">\n<p>.csharpcode, .csharpcode pre\n{\n\tfont-size: small;\n\tcolor: black;\n\tfont-family: consolas, \"Courier New\", courier, monospace;\n\tbackground-color: #ffffff;\n\t\/*white-space: pre;*\/\n}\n.csharpcode pre { margin: 0em; }\n.csharpcode .rem { color: #008000; }\n.csharpcode .kwrd { color: #0000ff; }\n.csharpcode .str { color: #006080; }\n.csharpcode .op { color: #0000c0; }\n.csharpcode .preproc { color: #cc6633; }\n.csharpcode .asp { background-color: #ffff00; }\n.csharpcode .html { color: #800000; }\n.csharpcode .attr { color: #ff0000; }\n.csharpcode .alt \n{\n\tbackground-color: #f4f4f4;\n\twidth: 100%;\n\tmargin: 0em;\n}\n.csharpcode .lnum { color: #606060; }<\/style>\n<\/p>\n<p><span style=\"font-size: 12pt\">\u6ce8\u91ca\u6389\u7684\u7a0b\u5e8f\u96c6\u5b58\u5728\u4e8eGAC\u4e2d, \u4f46\u662f\u6211\u4eec\u73b0\u5728\u4e0d\u5e0c\u671b\u4f7f\u7528GAC\u4e2d\u7684\u7a0b\u5e8f\u96c6, \u800c\u662f\u5f15\u7528\u9879\u76ee.<\/span>\n<\/p>\n<p><span style=\"font-size: 12pt\">3. \u5c06View\u76ee\u5f55\u4e0b\u7684Web.Config\u4e2d\u7684\u6240\u6709System.Web.Mvc\u76f8\u5173\u7684 PublicKeyToken \u90fd\u4fee\u6539\u4e3a null:<\/span>\n<\/p>\n<pre class=\"csharpcode\"><span style=\"font-size: 12pt\">    &lt;pages\r\n        validateRequest=<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;false&quot;<\/span>\r\n<span style=\"font-size: 12pt\">\r\n        pageParserFilterType=<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&quot;<\/span>\r\n<span style=\"font-size: 12pt\">\r\n        pageBaseType=<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;System.Web.Mvc.ViewPage, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&quot;<\/span>\r\n<span style=\"font-size: 12pt\">\r\n        userControlBaseType=<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&quot;<\/span>\r\n<span style=\"font-size: 12pt\">&gt;\r\n      &lt;controls&gt;\r\n        &lt;add assembly=<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null&quot;<\/span>\r\n <span class=\"kwrd\" style=\"font-size: 12pt\">namespace<\/span>\r\n<span style=\"font-size: 12pt\">=<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;System.Web.Mvc&quot;<\/span>\r\n<span style=\"font-size: 12pt\"> tagPrefix=<\/span>\r\n<span class=\"str\" style=\"font-size: 12pt\">&quot;mvc&quot;<\/span>\r\n<span style=\"font-size: 12pt\"> \/&gt;\r\n      &lt;\/controls&gt;\r\n    &lt;\/pages&gt;<\/span>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<h2><span style=\"font-size: 12pt\">\u4e00.\u6458\u8981<\/span><\/h2>\n<p><span style=\"font-size: 12pt\">\u4e00\u4e2aUrl\u8bf7\u6c42\u7ecf\u8fc7\u4e86Routing\u5904\u7406\u540e\u4f1a\u8c03\u7528Controller\u7684Action\u65b9\u6cd5. \u4e2d\u95f4\u7684\u8fc7\u7a0b\u662f\u600e\u6837\u7684? Action\u65b9\u6cd5\u4e2d\u8fd4\u56deActionResult\u5bf9\u8c61\u540e,\u5982\u4f55\u5230\u8fbeView\u7684? \u672c\u6587\u5c06\u8bb2\u89e3Controller\u7684\u57fa\u672c\u7528\u6cd5,&nbsp; \u6df1\u5165\u5206\u6790Controller\u7684\u8fd0\u884c\u673a\u5236, \u5e76\u4e14\u63d0\u4f9b\u4e86\u521b\u5efa\u6240\u6709\u7c7b\u578bAction\u7684\u4ee3\u7801. \u503c\u5f97\u5b66\u4e60ASP.NET MVC\u65f6\u53c2\u8003.<\/span><\/p>\n<h2><span style=\"font-size: 12pt\">\u4e8c.\u627f\u4e0a\u542f\u4e0b<\/span><\/h2>\n<p><span style=\"font-size: 12pt\">\u5728\u4e0a\u4e00\u7bc7\u6587\u7ae0\u4e2d, \u6211\u5df2\u7ecf\u5b66\u4f1a\u4e86\u5982\u4f55\u4f7f\u7528Routing\u83b7\u53d6Controller\u548cAction, \u968f\u540e\u7684\u7a0b\u5e8f\u4f1a\u8c03\u7528Controller\u4e2d\u7684Action\u65b9\u6cd5.<\/span><\/p>\n<p><span style=\"font-size: 12pt\">\u6bcf\u4e2aAction\u65b9\u6cd5\u90fd\u8981\u8fd4\u56de\u4e00\u4e2aActionResult\u5bf9\u8c61. \u4e00\u4e2aAction\u4f1a\u5c06\u6570\u636e\u4f20\u9012\u7ed9View,\u5982\u56fe:<\/span><\/p>\n<p><a href=\"http:\/\/images.cnblogs.com\/cnblogs_com\/zhangziqiu\/WindowsLiveWriter\/3903880e4390_7859\/image_2.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" alt=\"image\" border=\"0\" height=\"228\" src=\"http:\/\/images.cnblogs.com\/cnblogs_com\/zhangziqiu\/WindowsLiveWriter\/3903880e4390_7859\/image_thumb.png\" style=\"border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" title=\"image\" width=\"529\" \/><\/a><\/p>\n<h2><span style=\"font-size: 12pt\">\u4e09.Controller\u4e0eAction\u7684\u4f5c\u7528<\/span><\/h2>\n<h3><span style=\"font-size: 12pt\">1.\u804c\u8d23<\/span><\/h3>\n<p><span style=\"font-size: 12pt\">Controller\u8d1f\u8d23\u5c06\u83b7\u53d6Model\u6570\u636e\u5e76\u5c06Model\u4f20\u9012\u7ed9View\u5bf9\u8c61.\u901a\u77e5View\u5bf9\u8c61\u663e\u793a.<\/span><\/p>\n<h3><span style=\"font-size: 12pt\">2.ASP.NET MVC\u4e2d\u7684Controller\u548cAction<\/span><\/h3>\n<p><span style=\"font-size: 12pt\">\u5728ASP.NET MVC\u4e2d, \u4e00\u4e2aController\u53ef\u4ee5\u5305\u542b\u591a\u4e2aAction. \u6bcf\u4e00\u4e2aAction\u90fd\u662f\u4e00\u4e2a\u65b9\u6cd5, \u8fd4\u56de\u4e00\u4e2aAction...<\/span><\/p>\n<p> <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/128\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[273],"tags":[16,49],"class_list":["post-128","post","type-post","status-publish","format-standard","hentry","category-article","tag-C","tag-DotNet"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=128"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/128\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}