.NET CORE WebAPI 增加默認頁面功能

目錄

功能描述

核心配置代碼

核心代碼


功能描述

因爲.NET CORE WebAPI 是基於MVC 但是它主要是提供數據結構支持不提供VIEW操作,所以默認是不具備默認頁面顯示功能的;如果需要顯示默認頁面請增加代碼到 Startup 類中。

核心配置代碼

 public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        { 
            DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions();
            defaultFilesOptions.DefaultFileNames.Clear();
            defaultFilesOptions.DefaultFileNames.Add("index.html");
             
            //訪問HTML 靜態頁面
            app.UseStaticFiles();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            //跨域
            app.UseCors(configurePolicy => configurePolicy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());
            //使用 https 重定向功能
            app.UseHttpsRedirection();
            //使用 MVC
            app.UseMvc();

        }

核心代碼

 DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions();
            defaultFilesOptions.DefaultFileNames.Clear();
            defaultFilesOptions.DefaultFileNames.Add("index.html");
             
            //訪問HTML 靜態頁面
            app.UseStaticFiles();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章