{"id":1027,"date":"2022-04-08T08:22:56","date_gmt":"2022-04-08T00:22:56","guid":{"rendered":"http:\/\/www.wayln.com\/?p=1027"},"modified":"2022-04-08T14:50:35","modified_gmt":"2022-04-08T06:50:35","slug":"%e4%bb%8e%e5%a4%b4%e5%86%99asp-net-core-mvc%e9%a1%b9%e7%9b%ae%e6%b5%81%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=1027","title":{"rendered":"\u4ece\u5934\u5199ASP.NET CORE MVC\u9879\u76ee\u6d41\u7a0b"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_transparent no_bullets\"><p class=\"toc_title\">Contents<\/p><ul class=\"toc_list\"><li><a href=\"#i\"><span class=\"toc_number toc_depth_1\">1<\/span> \u65b0\u5efa\u9879\u76ee<\/a><\/li><li><a href=\"#i-2\"><span class=\"toc_number toc_depth_1\">2<\/span> \u65b0\u5efa\u6587\u4ef6\u7ed3\u6784<\/a><\/li><li><a href=\"#Startup\"><span class=\"toc_number toc_depth_1\">3<\/span> \u4fee\u6539Startup\u6587\u4ef6<\/a><\/li><\/ul><\/div>\n\n<h1><span id=\"i\">\u65b0\u5efa\u9879\u76ee<\/span><\/h1>\n<p>\u4f7f\u7528VS\u65b0\u5efaASP.NET CORE \u7a7a\u9879\u76ee<\/p>\n<h1><span id=\"i-2\">\u65b0\u5efa\u6587\u4ef6\u7ed3\u6784<\/span><\/h1>\n<ol>\n<li>\u6dfb\u52a0wwwroot\u6587\u4ef6\u5939<\/li>\n<li>\u6dfb\u52a0Controllers\u6587\u4ef6\u5939\n<ul>\n<li>\u5728Controllers\u6587\u4ef6\u5939\u4e2d\u6dfb\u52a0HomeController\u63a7\u5236\u5668\uff0c\u7ee7\u627f\u81eaController<\/li>\n<li>\u6dfb\u52a0using Microsoft.AspNetCore.Mvc;<\/li>\n<\/ul>\n<\/li>\n<li>\u6dfb\u52a0Views\u6587\u4ef6\u5939\n<ul>\n<li>\u5728Views\u6587\u4ef6\u5939\u4e2d\u5efa\u7acbHome\u6587\u4ef6\u5939\uff08\u5bf9\u5e94\u63a7\u5236\u5668\u7684\u540d\u5b57\uff09<br \/>\na. \u5982\u679c\u7528\u5230\u6a21\u677f\uff0c\u5219\u9700\u8981\u521b\u5efaDisplayTemplates\u6587\u4ef6\u5939\uff0c\u5728\u6b64\u6587\u4ef6\u4ef6\u4e0b\u521b\u5efa\u4e0e\u63a7\u5236\u5668\u540c\u540d\u7684\u89c6\u56fe\u6587\u4ef6\uff0c\u4f8b\u5982Home.cshtml<\/li>\n<li>\u5728Home\u6587\u4ef6\u5939\u4e2d\u6dfb\u52a0\u63a7\u5236\u5668\u4e2d\u5bf9\u5e94\u7684\u51fd\u6570\u540d\u7684CShtml\u6587\u4ef6\uff0c\u5982Index.cshtml<\/li>\n<li>\u6dfb\u52a0Shared\u6587\u4ef6\u5939<br \/>\na. \u5728Shared\u6587\u4ef6\u5939\u4e2d\u6dfb\u52a0_Layout.cshtml<br \/>\nb. \u5982\u679c\u9700\u8981\u7528\u5230ViewComponent,\u5219\u9700\u8981\u5728Shared\u6587\u4ef6\u5939\u4e2d\u6dfb\u52a0Components\u6587\u4ef6\u5939<br \/>\nc. \u5728Components\u6587\u4ef6\u5939\u4e2d\u6dfb\u52a0\u4e0e\u7ec4\u4ef6\u76f8\u540c\u540d\u5b57\u7684\u6587\u4ef6\u5939<br \/>\nd. \u5728C\u4e2d\u6587\u4ef6\u5939\u4e2d\u6dfb\u52a0\u89c6\u56fe<\/li>\n<li>\u6dfb\u52a0_ViewStart.cshtml,\u63a7\u5236\u6240\u6709\u9875\u9762\u6bcd\u7248\u9875<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<pre><code class=\"language-CSharp line-numbers\">@{\n    Layout = \"_Layout\";\n}\n<\/code><\/pre>\n<ul>\n<li>\u6dfb\u52a0_ViewImports.cshtml\uff0c\u5bfc\u5165taghelper,\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/li>\n<\/ul>\n<pre><code class=\"language-CSharp line-numbers\">@addTagHelper \"*,Microsoft.AspNetCore.Mvc.TagHelpers\"\n<\/code><\/pre>\n<ol start=\"4\">\n<li>\u6dfb\u52a0Models\u6587\u4ef6\u5939<\/li>\n<li>\u6dfb\u52a0Services\u6587\u4ef6\u5939<br \/>\n\u653e\u7f6e\u9700\u8981\u7684\u63a5\u53e3\u548c\u670d\u52a1<\/li>\n<li>\u6dfb\u52a0\u524d\u7aef\u5e93<br \/>\n\u5728\u9879\u76ee\u53f3\u952e\u9009\u62e9\u6dfb\u52a0\uff0c\u524d\u7aef\u5e93\uff0c\u7136\u540e\u9009\u62e9\u9879\u76ee\u9700\u8981\u7684\u524d\u7aef\u5e93\uff0c\u63d0\u4f9b\u7a0b\u5e8f\u9009\u62e9nupkg,\u4f7f\u7528libman\u7ba1\u7406<\/li>\n<\/ol>\n<h1><span id=\"Startup\">\u4fee\u6539Startup\u6587\u4ef6<\/span><\/h1>\n<pre><code class=\"language-CSharp line-numbers\"> public class Startup\n    {\n\n        public void ConfigureServices(IServiceCollection services)\n        {\n            services.AddControllersWithViews();\n        }\n\n        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)\n        {\n            if (env.IsDevelopment())\n            {\n                app.UseDeveloperExceptionPage();\n            }\n\n\n            app.UseStaticFiles();\n\n            app.UseRouting();\n\n\n            app.UseEndpoints(endpoints =&gt;\n            {\n                endpoints.MapControllerRoute(name: \"default\", pattern: \"{controller=Home}\/{action=Index}\/{id?}\");\n            });\n        }\n    }\n<\/code><\/pre>\n<ol start=\"7\">\n<li>\u6dfb\u52a0ViewComponents\u6587\u4ef6\u5939<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u65b0\u5efa\u9879\u76ee2 \u65b0\u5efa\u6587\u4ef6\u7ed3\u67843 \u4fee\u6539Startup\u6587\u4ef6 \u65b0\u5efa\u9879\u76ee \u4f7f\u7528VS\u65b0\u5efaASP.NET  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44,4,2],"tags":[],"class_list":["post-1027","post","type-post","status-publish","format-standard","hentry","category-asp-net-core","category-c","category-2"],"_links":{"self":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/1027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1027"}],"version-history":[{"count":15,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/1027\/revisions"}],"predecessor-version":[{"id":1050,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/1027\/revisions\/1050"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}