{"id":990,"date":"2022-04-07T21:18:23","date_gmt":"2022-04-07T13:18:23","guid":{"rendered":"http:\/\/www.wayln.com\/?p=990"},"modified":"2022-04-07T22:56:50","modified_gmt":"2022-04-07T14:56:50","slug":"asp-net-core-%e9%a1%b9%e7%9b%ae%e8%af%b4%e6%98%8e","status":"publish","type":"post","link":"https:\/\/www.wayln.com\/?p=990","title":{"rendered":"ASP.NET Core \u9879\u76ee\u8bf4\u660e"},"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> \u9879\u76ee<\/a><ul><li><a href=\"#i-2\"><span class=\"toc_number toc_depth_2\">1.1<\/span> \u6587\u4ef6<\/a><ul><li><a href=\"#Startup\"><span class=\"toc_number toc_depth_3\">1.1.1<\/span> Startup<\/a><ul><li><a href=\"#Configure\"><span class=\"toc_number toc_depth_4\">1.1.1.1<\/span> Configure<\/a><\/li><li><a href=\"#ConfigureServices\"><span class=\"toc_number toc_depth_4\">1.1.1.2<\/span> ConfigureServices<\/a><\/li><\/ul><\/li><li><a href=\"#launchSettingsjson\"><span class=\"toc_number toc_depth_3\">1.1.2<\/span> launchSettings.json<\/a><\/li><li><a href=\"#wwwroot\"><span class=\"toc_number toc_depth_3\">1.1.3<\/span> wwwroot<\/a><\/li><li><a href=\"#css\"><span class=\"toc_number toc_depth_3\">1.1.4<\/span> css<\/a><\/li><li><a href=\"#Controller\"><span class=\"toc_number toc_depth_3\">1.1.5<\/span> Controller\u6587\u4ef6\u5939<\/a><\/li><li><a href=\"#Views\"><span class=\"toc_number toc_depth_3\">1.1.6<\/span> Views\u6587\u4ef6\u5939<\/a><ul><li><a href=\"#i-3\"><span class=\"toc_number toc_depth_4\">1.1.6.1<\/span> \u4e0e\u63a7\u5236\u5668\u540c\u540d\u7684\u6587\u4ef6\u5939<\/a><ul><li><a href=\"#view\"><span class=\"toc_number toc_depth_5\">1.1.6.1.1<\/span> \u63a7\u5236\u5668\u4e2d\u7684\u51fd\u6570\u540d\u7684view<\/a><\/li><\/ul><\/li><li><a href=\"#Shared\"><span class=\"toc_number toc_depth_4\">1.1.6.2<\/span> Shared\u6587\u4ef6\u5939<\/a><\/li><li><a href=\"#__ViewStartscshtml\"><span class=\"toc_number toc_depth_4\">1.1.6.3<\/span> _ ViewStarts.cshtml<\/a><\/li><li><a href=\"#__ViewImportscshtml\"><span class=\"toc_number toc_depth_4\">1.1.6.4<\/span> _ ViewImports.cshtml<\/a><\/li><\/ul><\/li><\/ul><\/li><li><a href=\"#i-4\"><span class=\"toc_number toc_depth_2\">1.2<\/span> \u5305\u7ba1\u7406<\/a><ul><li><a href=\"#i-5\"><span class=\"toc_number toc_depth_3\">1.2.1<\/span> \u524d\u7aef\u5e93\u7ba1\u7406\u5de5\u5177<\/a><ul><li><a href=\"#npm\"><span class=\"toc_number toc_depth_4\">1.2.1.1<\/span> npm<\/a><\/li><li><a href=\"#_libmanjson\"><span class=\"toc_number toc_depth_4\">1.2.1.2<\/span> \u5ba2\u6237\u7aef\u5e93 libman.json<\/a><\/li><\/ul><\/li><\/ul><\/li><li><a href=\"#css-2\"><span class=\"toc_number toc_depth_2\">1.3<\/span> css\u6587\u4ef6\u538b\u7f29\u548c\u5408\u5e76<\/a><\/li><\/ul><\/li><\/ul><\/div>\n<p>[TOC]<\/p>\n<h1><span id=\"i\">\u9879\u76ee<\/span><\/h1>\n<h2><span id=\"i-2\">\u6587\u4ef6<\/span><\/h2>\n<h3><span id=\"Startup\">Startup<\/span><\/h3>\n<h4><span id=\"Configure\">Configure<\/span><\/h4>\n<p>app\u8d1f\u8d23\u914d\u7f6e\u7ba1\u9053\uff0c\u7ef4\u62a4\u7ba1\u9053\u4e2d\u7684\u4e2d\u95f4\u4ef6\uff0c\u4e2d\u95f4\u4ef6\u6ce8\u518c\u7684\u987a\u5e8f\u5f88\u91cd\u8981<\/p>\n<pre><code class=\"language-CSharp line-numbers\">public void Configure(IApplicationBuilder app,IWebHostEnvironment env)\n<\/code><\/pre>\n<p>\u5982\u679c\u662f\u5f00\u53d1\u73af\u5883\uff0c\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u5982\u4e0b\u4ee3\u7801\uff0c\u5c31\u4e0d\u4f1a\u7528\u4e0a\u9762\u7684Configure\u65b9\u6cd5\u4e86\uff0c\u5982\u679c\u662f\u751f\u4ea7\u73af\u5883\uff0c\u627e\u4e0d\u5230ConfigureProduct\u65b9\u6cd5\uff0c\u5219\u4f1a\u7ee7\u7eed\u4f7f\u7528\u4e0a\u9762\u7684Configure\u65b9\u6cd5<\/p>\n<pre><code class=\"language-CSharp line-numbers\">public void ConfigureDeVelopment(IApplicationBuilder app,IWebHostEnvironment env)\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u73af\u5883\u7684Startup\u65b9\u6cd5,\u5f00\u53d1\u73af\u5883\u6539\u540d\u4e3aStartUpDevelopment,\u7136\u540e\u518dProgram.CS\u4e2d\u4fee\u6539\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">        public static IHostBuilder CreateHostBuilder(string[] args) =&gt;\n            Host.CreateDefaultBuilder(args)\n                .ConfigureWebHostDefaults(webBuilder =&gt;\n                {\n                  \/\/  webBuilder.UseStartup&lt;Startup&gt;();\n                    webBuilder.UseStartup(typeof(Program));\n                });\n<\/code><\/pre>\n<p><strong>\u6bd4\u8f83\u91cd\u8981\u7684\u4e2d\u95f4\u4ef6\u6709\uff1a<\/strong><br \/>\n1. app.UseStaticFiles()  \/\/\u8d1f\u8d23\u5f00\u542f\u9759\u6001\u6587\u4ef6\u8c03\u7528<br \/>\n2. app.UseHttosRedirection()  \/\/\u5f3a\u5236\u4f7f\u7528https<br \/>\n2. app.UseAuthentication()  \/\/\u9a8c\u8bc1\u6743\u9650<\/p>\n<h4><span id=\"ConfigureServices\">ConfigureServices<\/span><\/h4>\n<p>\u8d1f\u8d23\u4f9d\u8d56\u6ce8\u5165\uff0cIOC\u5bb9\u5668<\/p>\n<pre><code class=\"language-CSharp line-numbers\">        public void ConfigureServices(IServiceCollection services)\n        {\n            \/\/\u542f\u7528MVC\n            services.AddControllersWithViews();\n        }\n<\/code><\/pre>\n<h3><span id=\"launchSettingsjson\">launchSettings.json<\/span><\/h3>\n<p>\u5728\u9879\u76ee\u6587\u4ef6\u5939\u4e0b\u7684Properties\u6587\u4ef6\u5939\u4e2d\uff0c\u914d\u7f6e\u73af\u5883\u53d8\u91cf\u7b49<br \/>\n\u5f00\u53d1\u73af\u5883\uff1a<br \/>\n1. Development<\/p>\n<h3><span id=\"wwwroot\">wwwroot<\/span><\/h3>\n<p>\u6240\u6709\u7684\u9759\u6001\u6587\u4ef6\u9700\u8981\u653e\u5230wwwroot\u6587\u4ef6\u5939\u4e2d\uff0c\u4f8b\u5982css\u6587\u4ef6\uff0cjs\u6587\u4ef6\uff0c\u56fe\u7247\uff0c\u56fe\u6807\uff0c\u5b57\u4f53\u7b49<br \/>\n<strong>\u9700\u8981\u518dConfigure\u65b9\u6cd5\u4e2d\u4f7f\u7528app.UseStaticFiles()\u4e2d\u95f4\u4ef6\uff0c\u5426\u5219\u65e0\u6cd5\u8bbf\u95ee\u9759\u6001\u6587\u4ef6<\/strong><\/p>\n<h3><span id=\"css\">css<\/span><\/h3>\n<p>\u5b58\u653e\u9879\u76ee\u9700\u8981\u7684CSS\u8bbe\u7f6e\u6587\u4ef6\uff0c\u53ef\u4ee5\u65b0\u5efa\u4e00\u4e2asite.css<\/p>\n<h3><span id=\"Controller\">Controller\u6587\u4ef6\u5939<\/span><\/h3>\n<p>\u5b58\u653e\u63a7\u5236\u5668\u6587\u4ef6<\/p>\n<h3><span id=\"Views\">Views\u6587\u4ef6\u5939<\/span><\/h3>\n<p>\u5b58\u653e\u89c6\u56fe\u6587\u4ef6<\/p>\n<h4><span id=\"i-3\">\u4e0e\u63a7\u5236\u5668\u540c\u540d\u7684\u6587\u4ef6\u5939<\/span><\/h4>\n<p>\u5982\u63a7\u5236\u5668\u662fHomeControler\uff0c\u5219\u8be5\u6587\u4ef6\u5939\u4e3aHome<\/p>\n<h5><span id=\"view\">\u63a7\u5236\u5668\u4e2d\u7684\u51fd\u6570\u540d\u7684view<\/span><\/h5>\n<p>\u5982Home\u63a7\u5236\u5668\u4e2d\u7684Index()\u51fd\u6570\uff0c\u5219\u9700\u8981\u5efa\u7acbIndex.cshtml\u89c6\u56fe<\/p>\n<h4><span id=\"Shared\">Shared\u6587\u4ef6\u5939<\/span><\/h4>\n<p>\u5b58\u653e\u516c\u5171\u89c6\u56fe\u6587\u4ef6\uff0c\u5982_layout.cshtml,\u4f5c\u4e3a\u6bcd\u7248\u9875\uff0c\u53ef\u4ee5\u516c\u7528<\/p>\n<h4><span id=\"__ViewStartscshtml\">_ ViewStarts.cshtml<\/span><\/h4>\n<p>\u8bbe\u7f6e\u6240\u6709\u9875\u9762\u516c\u5171\u4ee3\u7801\uff0c<\/p>\n<pre><code class=\"language-CSharp line-numbers\">@{\n    Layout=\"_Layout\";\n}\n<\/code><\/pre>\n<h4><span id=\"__ViewImportscshtml\">_ ViewImports.cshtml<\/span><\/h4>\n<p>\u5728\u5168\u9879\u76ee\u4e2d\u6dfb\u52a0taghelper\u5f15\u7528\uff0c\u5982\u679c\u4e0d\u60f3\u5728\u5168\u5c40\u4e2d\u6dfb\u52a0\uff0c\u53ef\u4ee5\u5728\u6bcf\u4e00\u4e2aView\u4e2d\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-CSharp line-numbers\">@addTagHelper \"*,Microsoft.AspNetCore.Mvc.TagHelpers\"\n<\/code><\/pre>\n<h2><span id=\"i-4\">\u5305\u7ba1\u7406<\/span><\/h2>\n<ul>\n<li>\u670d\u52a1\u5668\u7aef\uff08\u540e\u7aef\uff09\uff1aNuget<\/li>\n<li>\u524d\u7aef\uff1aNpm<\/li>\n<\/ul>\n<h3><span id=\"i-5\">\u524d\u7aef\u5e93\u7ba1\u7406\u5de5\u5177<\/span><\/h3>\n<h4><span id=\"npm\">npm<\/span><\/h4>\n<p>npm\u9700\u8981\u518d\u9879\u76ee\u4e2d\u6dfb\u52a0npm ConfigurationFile\u7684json\u914d\u7f6e\u6587\u4ef6<br \/>\n\u6dfb\u52a0\u9700\u8981\u7684\u5305<\/p>\n<pre><code class=\"language-json line-numbers\">{\n  \"version\": \"1.0.0\",\n  \"name\": \"asp.net\",\n  \"private\": true,\n  \"devDependencies\": {\n    \"bootstrap\": \"5.1.3\"\n  }\n}\n<\/code><\/pre>\n<h4><span id=\"_libmanjson\">\u5ba2\u6237\u7aef\u5e93 libman.json<\/span><\/h4>\n<p>\u5728VS\u9879\u76ee\u4e0a\u53f3\u952e\uff0c\u70b9\u51fb\u5ba2\u6237\u7aef\u5e93\uff0c\u7136\u540e\u9009\u62e9\u9700\u8981\u7684\u5e93,\u4f1a\u5728\u9879\u76ee\u4e2d\u6dfb\u52a0libman.js<br \/>\n<a class=\"wp-editor-md-post-content-link\" href=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2022\/04\/wp_editor_md_a9dce9c4c69cbbd5c78dc9e9b50d14fb.jpg\"><img decoding=\"async\" src=\"http:\/\/www.wayln.com\/wp-content\/uploads\/2022\/04\/wp_editor_md_a9dce9c4c69cbbd5c78dc9e9b50d14fb.jpg\" alt=\"\" \/><\/a><\/p>\n<h2><span id=\"css-2\">css\u6587\u4ef6\u538b\u7f29\u548c\u5408\u5e76<\/span><\/h2>\n<p>\u6dfb\u52a0json\u6587\u4ef6\uff0c\u547d\u540d\u4e3abundleconfig.json<br \/>\n\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-json line-numbers\">[\n  {\n    \"outputFileName\": \"wwwroot\/css\/all.min.css\",\n    \"inputFiles\": [ \"wwwroot\/lib\/bootstrap\/css\/bootstrap.css\", \"wwwroot\/css\/site.css\" ]\n  },\n  {\n    \"outputFileName\": \"wwwroot\/css\/bootstrap.css\",\n    \"inputFiles\": [ \"wwwroot\/lib\/bootstrap\/css\/bootstrap.css\" ],\n    \"minify\": {\"enabled\": true}\n  }\n\n]\n<\/code><\/pre>\n<p>\u901a\u8fc7nuget\u5305\u5b89\u88c5BuildBundlerMinifier\uff0c\u7136\u540e\u7f16\u8bd1\u9879\u76ee\uff0c\u4f1a\u6267\u884c\u538b\u7f29\u5408\u5e76<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Contents1 \u9879\u76ee1.1 \u6587\u4ef61.1.1 Startup1.1.1.1 Configure1.1.1.2 [&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],"tags":[],"class_list":["post-990","post","type-post","status-publish","format-standard","hentry","category-asp-net-core"],"_links":{"self":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/990","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=990"}],"version-history":[{"count":24,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/990\/revisions"}],"predecessor-version":[{"id":1020,"href":"https:\/\/www.wayln.com\/index.php?rest_route=\/wp\/v2\/posts\/990\/revisions\/1020"}],"wp:attachment":[{"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wayln.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}