ASP.NET Core MVC Tag Helpers

环境 Environment

ASPNETCORE_ENVIRONMENT:

• Development 开发环境
• Staging 准备上线
• Production 生产环境

根据环境调用不同的方法:
• ConfigureServicesDevelopment()
• ConfigureServicesStaging()
• ConfigureServicesProduction()
• ConfigureDevelopment()
• ConfigureStaging()
• ConfigureProduction()

根据环境调用不同的 Startup 类:
• StartupDevelopment
• StartupStaging
• StartupProduction

如需使用不同的 Startup 类,需先在 program 里面稍作修改:

public static void Main(string[] args)
{
    CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args)
{
    var assemblyName = typeof(Startup).GetTypeInfo().Assembly.FullName;

    return WebHost.CreateDefaultBuilder(args)
        .UseStartup(assemblyName);
}
MVC

• Controller 控制器
• Action
• Filter 只适用于mvc
• Model Binding
• Routing
• Attribute

启动文件配置:

public class Startup
    {

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();//使用该方法后才可以使用app.UseMvc()方法
            services.AddSingleton<Ifactory, Factory>();  
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
           // app.UseStatusCodePages(); //返回静态页面

            app.UseStaticFiles();//提供静态的文件服务
            app.UseMvc(
                        (routes) =>
                        {
                            routes.MapRoute(
                                name: "default",
                                template: "{controller=Home}/{action=Index}/{id?}"
                                           );
                        });

        }
    }
![](https://img2020.cnblogs.com/blog/2187286/202010/2187286-20201025171430698-953281235.png)
shated文件夹下是模板 (Views文件夹添加 _Viewstart(Razor视图开始)自生成配置)

全局引用Tag Helper

<!--Views目录下新建——ViewStart.cshtml(Razor视图导入)-->
@*全局引入所有微软MVC提供的Tag Helpers*@
@addTagHelper "*,Microsoft.AspNetCore.Mvc.TagHelpers"

前端不同环境下引用不同的CSS文件
前端图片缓存问题(服务器图片已经更换,客户端显示的依然是以前版本的图片)
asp-append-version (在变量后添加一个微变量值)
支持Img、Script、Link
静态文件发生改变,Tag Helper就重新计算文件的HASH,它采用 SHA256的哈希值
HASH值不同,缓存的文件版本便无效了

<!DOCTYPE html>
<!--以下代码位置在Shared模板里-->
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>@ViewBag.Title</title>
    <link rel="stylesheet" href="~/css/StyleSheet.css" />

    <!--------------------------------------------------------------------------------------------------->
    <!-- environment实现在不同环境中引用不同的css-->
    <!-- asp-append-version 避免图片缓存的问题 -->
    <environment include="Development">
        <link rel="stylesheet" asp-href-include="css/*" asp-href-exclude="css/all.min.css" />
        <link href="css/site.css" rel="stylesheet" />
    </environment>

    <!--------------------------------------------------------------------------------------------------->
    <!--除了开发黄环境以外-->>
    <environment exclude="Development">
        <link rel="stylesheet" asp-href-include="css/all.min.css" />
    </environment>

    <!--------------------------------------------------------------------------------------------------->
    <!-- Staging (模拟)境中对 css 的引用 -->
    <environment include="Staging">
        <link rel="stylesheet" asp-href-include="css/all.min.css" />
    </environment>

</head>
<body>
    <nav class="navbar navbar-light bg-light">
        <a class="navbar-brand" href="#">
            <img asp-append-version="true" src="~/img/bootstrap-solid.svg" width="30" height="30" class="d-inline-block align-top" alt="" loading="lazy">
            Bootstrap
        </a>
    </nav>
    <div>
        @RenderBody()

    </div>
</body>
</html>

Shared文件夹下 (按照约定,在多个 Razor Pages 上共享的分部标记元素位于 Pages/Shared 目录)

_Layout.cshtml:在多个 Razor Pages 中提供公共布局元素。
_CookieConsentPartial.cshtml:提供此项目的所有 Razor Pages 中包含的 cookie 许可警报和功能。
_ValidationScriptsPartial.cshtml:提供此项目中所有 Razor Pages 可用的验证功能,如客户端窗体输入验证和跨站点防伪验证。

posted @ 2020-10-25 17:22  李花花小番茄  阅读(79)  评论(0编辑  收藏  举报