为abp的application service 添加Swagger的Restful api 描述, 并动态生成web api

时间:2024-10-12 14:01:44
    public class Startup
    {
        private readonly IConfigurationRoot _appConfiguration;
        public Startup(IHostingEnvironment env)
        {
            _appConfiguration = ();
        }
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            //Configure DbContext
            <SCWebApiDbContext>(options =>
            {
                (, );
            });
            (options =>
            {
                ( new AutoValidateAntiforgeryTokenAttribute());
            });
            // Swagger - Enable this line and the related lines in Configure method to enable swagger UI
            (options =>
            {
                ("v1", new Info { Title = "SC WebApi", Version = "v1" });
                ((docName, description) => true);              
            });
            //Configure Abp and Dependency Injection
            return <SCWebApiWebModule>(options =>
            {
                //Configure Log4Net logging
                <LoggingFacility>(
                    f => f.UseAbpLog4Net().WithConfig( "" )
                );
            });
        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            (); //Initializes ABP framework.
           
            if (())
            {
                ();
                ();
            }
            else
            {
                ( "/Error" );
            }
            ();
            (routes =>
            {
                (
                   name: "defaultWithArea" ,
                   template: "{area}/{controller=Home}/{action=Index}/{id?}" );
                (
                    name: "default" ,
                    template: "{controller=Home}/{action=Index}/{id?}" );
            });
            // Enable middleware to serve generated Swagger as a JSON endpoint
            ();
            // Enable middleware to serve swagger-ui assets (HTML, JS, CSS etc.)
            (options =>
            {
                (_appConfiguration["App:ServerRootAddress"] + "/swagger/v1/", "SC API V1");
                = () => ()
                    .GetManifestResourceStream("");
            }); // URL: /swagger
        }
    }