{"id":7073,"date":"2023-09-22T17:06:17","date_gmt":"2023-09-22T09:06:17","guid":{"rendered":"https:\/\/i007.vip:15443\/wordpress\/?p=7073"},"modified":"2023-09-22T17:06:17","modified_gmt":"2023-09-22T09:06:17","slug":"c-mvc-5-%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f%ef%bc%88%e5%ba%94%e7%94%a8%e7%a8%8b%e5%ba%8f%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f%e8%af%b7%e6%b1%82%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f%ef%bc%89","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/7073","title":{"rendered":"C# MVC 5 &#8211; \u751f\u547d\u5468\u671f\uff08\u5e94\u7528\u7a0b\u5e8f\u751f\u547d\u5468\u671f&#038;\u8bf7\u6c42\u751f\u547d\u5468\u671f\uff09"},"content":{"rendered":"<div class=\"postText\">\n<div id=\"cnblogs_post_body\" class=\"blogpost-body blogpost-body-html\">\n<p><a href=\"https:\/\/www.cnblogs.com\/alunchen\/p\/6252846.html\">cc from<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>1.\u4ecb\u7ecd<\/strong><\/p>\n<p>\u672c\u6587\u8ba8\u8bbaASP.Net MVC\u6846\u67b6MVC\u7684\u8bf7\u6c42\u751f\u547d\u5468\u671f\u3002<\/p>\n<p>MVC\u6709\u4e24\u4e2a\u751f\u547d\u5468\u671f\uff0c\u4e00\u4e3a\u5e94\u7528\u7a0b\u5e8f\u751f\u547d\u5468\u671f\uff0c\u4e8c\u4e3a\u8bf7\u6c42\u751f\u547d\u5468\u671f\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>2.\u5e94\u7528\u7a0b\u5e8f\u751f\u547d\u5468\u671f<\/strong><\/p>\n<p><a href=\"http:\/\/images2015.cnblogs.com\/blog\/914305\/201701\/914305-20170105162318284-945177908.png\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" src=\"https:\/\/images2015.cnblogs.com\/blog\/914305\/201701\/914305-20170105162319003-1403691718.png\" alt=\"image\" width=\"706\" height=\"276\" border=\"0\" \/><\/a><\/p>\n<p>\u5e94\u7528\u7a0b\u5e8f\u751f\u547d\u5468\u671f\u662f\u6307\u5728\u7533\u8bf7\u8fc7\u7a0b\u5b9e\u9645\u5f00\u59cb\u8fd0\u884cIIS\uff0c\u76f4\u5230\u5b83\u505c\u6b62\u7684\u65f6\u95f4\u3002\u8fd9\u662f\u901a\u8fc7\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u542f\u52a8\u6587\u4ef6\u7684\u5e94\u7528\u7a0b\u5e8f\u5f00\u59cb\u548c\u7ed3\u675f\u6807\u8bb0\u4e8b\u4ef6\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>3.\u8bf7\u6c42\u751f\u547d\u5468\u671f<\/strong><\/p>\n<p>\u53d1\u751f\u6bcf\u4e00HTTP\u8bf7\u6c42\u662f\u7531\u6211\u4eec\u7684\u5e94\u7528\u5904\u7406\u7684\u65f6\u95f4\u7684\u4e8b\u4ef6\u5e8f\u5217\u3002<\/p>\n<p>\u5165\u53e3\u70b9\u4e3a\u6bcf\u4e2aMVC\u5e94\u7528\u59cb\u7ec8\u7531ASP.NET\u6536\u5230\u8bf7\u6c42\u540e\uff0c\u8ba1\u7b97\u51fa\u5e94\u8be5\u5982\u4f55\u901a\u8fc7URL\u8def\u7531\u6a21\u5757\u6765\u5904\u7406\u3002<\/p>\n<p><a href=\"http:\/\/images2015.cnblogs.com\/blog\/914305\/201701\/914305-20170105162319753-918433131.png\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" src=\"https:\/\/images2015.cnblogs.com\/blog\/914305\/201701\/914305-20170105162320300-797189412.png\" alt=\"image\" width=\"710\" height=\"281\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>1\uff09Routing \u8def\u7531<\/strong><\/p>\n<p>ASP.NET MVC\u751f\u547d\u5468\u671f\u4e2d\u7b2c\u4e00\u4e2a\u6b65\u9aa4\u5c31\u662f\u8def\u7531\uff0c\u8def\u7531\u7684\u8fc7\u7a0b\u662f\u6839\u636e\u8bf7\u6c42\u7684Url\u548c\u6ce8\u518c\u597d\u7684\u8def\u7531\u89c4\u5219\u8fdb\u884c\u5339\u914d\uff0c\u5f53\u4e00\u65e6\u627e\u5230\u4e00\u4e2a\u4e0e\u4e4b\u5339\u914d\u627e\u8def\u7531\u89c4\u5219\u65f6\uff0cASP.NET MVC\u8def\u7531\u5f15\u64ce\u5c31\u628a\u8fd9\u4e2a\u8bf7\u6c42\u4ea4\u7ed9\u76f8\u5e94\u7684Handler\u5904\u7406\u3002\u5982\u679c\u6ca1\u5339\u914d\uff0c\u5219\u8fd4\u56de404\u9519\u8bef\u3002<\/p>\n<p>\u6ce8\u518c\u8def\u7531\uff0c\u9996\u5148\u4eceGlobal.asax\u4e0a\u9762\u6ce8\u518c\uff1a<\/p>\n<p><a href=\"http:\/\/images2015.cnblogs.com\/blog\/914305\/201701\/914305-20170105162320909-1820123084.png\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" src=\"https:\/\/images2015.cnblogs.com\/blog\/914305\/201701\/914305-20170105162321300-1566219798.png\" alt=\"image\" width=\"574\" height=\"180\" border=\"0\" \/><\/a><\/p>\n<p>\u7136\u540e\u6211\u4eec\u770b\u5230RouteConfig.cs\u7684\u914d\u7f6e\uff1a<\/p>\n<div class=\"cnblogs_code\">\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<pre>   public class RouteConfig\r\n    {\r\n        public static void RegisterRoutes(RouteCollection routes)\r\n        {\r\n            routes.IgnoreRoute(\"{resource}.axd\/{*pathInfo}\");\r\n\r\n            routes.MapRoute(\r\n                name: \"Default\",\r\n                url: \"{controller}\/{action}\/{id}\",\r\n                defaults: new { action = \"Index\", id = UrlParameter.Optional }\r\n            );\r\n        }\r\n    }<\/pre>\n<div class=\"cnblogs_code_toolbar\"><span class=\"cnblogs_code_copy\"><a title=\"\u590d\u5236\u4ee3\u7801\"><img decoding=\"async\" src=\"https:\/\/common.cnblogs.com\/images\/copycode.gif\" alt=\"\u590d\u5236\u4ee3\u7801\" \/><\/a><\/span><\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p><strong>2\uff09Controller \u63a7\u5236\u5668<\/strong><\/p>\n<p>Controller\u662fMVC\u6700\u6838\u5fc3\u7684\u7ec4\u4ef6\uff0c\u4ed6\u662f\u8fde\u63a5View\u4e0eModel\u7684\u6865\u6881\uff0c\u5728Controller\u5185\u90e8\u4f1a\u6709\u76f8\u5e94\u7684Action\u65b9\u6cd5\uff0c\u53ef\u4ee5\u51b3\u5b9a\u8bf7\u6c42\u8fd4\u56de\u7684\u89c6\u56fe\u6216\u6570\u636e\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>3\uff09Action Excution<\/strong><\/p>\n<p>\u4e00\u65e6Controller\u88ab\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4ee5\u540e\uff0c\u91cc\u9762\u7684Action\u5c31\u5f00\u59cb\u4e0a\u573a\u4e86\u3002\u8fd9\u91ccASP.NET MVC\u7684\u6846\u67b6\u7684\u4e00\u4e2a\u6838\u5fc3\u7ec4\u4ef6ActionInvoker\u5c31\u51b3\u5b9a\u4e86\u8bf7\u6c42\u5230\u5e95\u662f\u54ea\u4e00\u4e2aAction\u7684\u65b9\u6cd5\u3002Action\u65b9\u6cd5\u662f\u771f\u6b63\u63a5\u6536\u8bf7\u6c42\u7684\u5904\u7406\u65b9\u6cd5\u3002<\/p>\n<p>Action\u63a5\u6536\u7528\u6237\u7684\u8f93\u5165\uff0c\u8fdb\u884c\u76f8\u5e94\u7684\u903b\u8f91\u5904\u7406\u5e76\u628a\u8981\u8fd4\u56de\u7684\u6570\u636eModel\u51c6\u5907\u597d\u4e86\u4e4b\u540e\u4f20\u7ed9\u76f8\u5e94\u7684\u89c6\u56fe\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4\uff09View Engine \u89c6\u56fe\u5f15\u64ce<\/strong><\/p>\n<p>Action\u65b9\u6cd5\u7684\u4f5c\u7528\u662f\u63a5\u53d7\u8f93\u5165\uff0c\u7136\u540e\u8fdb\u884c\u4e00\u5b9a\u7684\u5904\u7406\u5e76\u51c6\u5907\u54cd\u5e94\u6570\u636e\uff0c\u6700\u540e\u8fd4\u56de\u4e00\u4e2aView Result\u7c7b\u578b\u5b9e\u4f8b\u3002\u8fd4\u56de\u7684View Result\u7c7b\u578b\u8fd9\u4e9b\u7c7b\u578b\uff1aViewResult, RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, FileResult, and EmptyResult\u7b49\u7b49\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u5728Action\u65b9\u6cd5\u7684\u6700\u540e\u8fd4\u56de\u4e00\u4e2aView Result\u7684\u65f6\u5019\uff0cASP.NET MVC\u6846\u67b6\u9996\u5148\u5c31\u4f1a\u9009\u62e9\u76f8\u5e94\u7684\u89c6\u56fe\u5f15\u64ce\u6765\u8fd4\u56de\u89c6\u56fe\u3002ASP.NET\u89c6\u56fe\u5f15\u64ce\u90fd\u4f1a\u5b9e\u73b0\u63a5\u53e3IViewEngine\u3002\u9ed8\u8ba4ASP.NET MVC\u6846\u67b6\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u63a5\u53e3IViewEngine\u4e24\u4e2a\u5b9e\u73b0\uff0c\u89c6\u56fe\u5f15\u64ceWebForm\u548cRazor\u3002<\/p>\n<p>View\u89c6\u56fe\u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u6a21\u677f\u7684\u4f5c\u7528\uff0c\u5b83\u662f\u628aAction\u4f20\u8fc7\u6765\u7684\u6570\u636e\u7ed1\u5b9a\u5230\u89c6\u56fe\u6587\u4ef6\u4e2d\u5e76\u663e\u793a\u5230\u9875\u9762\u4e2d\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u53e6\u9644\u4e0a\u4e00\u4e2a\u7f51\u4e0a\u7684MVC\u8bf7\u6c42\u751f\u547d\u751f\u547d\u5468\u671f\u56fe\uff0c\u4f9b\u5927\u5bb6\u66f4\u597d\u5730\u7406\u89e3\uff1a<\/p>\n<p><a href=\"http:\/\/images2015.cnblogs.com\/blog\/914305\/201701\/914305-20170105162321956-310025417.png\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" title=\"image\" src=\"https:\/\/images2015.cnblogs.com\/blog\/914305\/201701\/914305-20170105162322659-794156659.png\" alt=\"image\" width=\"583\" height=\"448\" border=\"0\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>\u53ef\u4ee5\u5173\u6ce8\u672c\u4eba\u7684\u516c\u4f17\u53f7\uff0c\u591a\u5e74\u7ecf\u9a8c\u7684\u539f\u521b\u6587\u7ae0\u5171\u4eab\u7ed9\u5927\u5bb6\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img2018.cnblogs.com\/blog\/914305\/201810\/914305-20181015182707033-510683802.png\" alt=\"\" \/><\/p>\n<\/div>\n<div id=\"MySignature\" role=\"contentinfo\">alunchen<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>cc from &nbsp; 1.\u4ecb\u7ecd <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[24],"tags":[121],"class_list":["post-7073","post","type-post","status-publish","format-standard","hentry","category-value_docs","tag-05-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/7073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=7073"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/7073\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=7073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=7073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=7073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}