繁体   English   中英

从asp.net核心控制台应用程序运行网站

[英]Run Website from asp.net core console app

我有一个控制台应用程序,它将是一个服务,它有一个网站来配置服务。

我已经建立了我的网站,当我运行该项目时它工作正常。

我在控制台应用程序中引用了我的web项目,在那里我使用以下代码来启动网站。

    Task.Factory.StartNew(new Action(() => Website.Program.Main(null)), TaskCreationOptions.LongRunning);

我在网站程序类中的代码是

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

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .UseUrls("http://0.0.0.0:1234");              
}

我的问题是网站现在无法在wwwRoot中找到css文件和javascript文件,我尝试将应用程序发布到控制台应用程序的调试文件夹,但这不起作用。 如果我不发布,控制台应用程序的调试文件夹中只有dll文件,如果我发布所有文件,但视图无法找到它们。

我在视图中使用〜来查找文件。

理想情况下,我想要一个可以安装自己的网站和API的服务,而不会在本地计算机IIS中显示或需要通过Windows功能启用IIS。

编辑Startup.cs

  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.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });
        services.AddAuthentication(opt =>
        {
            opt.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            opt.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            opt.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        }).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
        {
            options.Cookie.Name = "TmiginScheme";
            options.LoginPath = "/Account/Login";
            options.LogoutPath = "/Account/Logout";
            options.AccessDeniedPath = "/Home/AccessDenied";
            options.ExpireTimeSpan = TimeSpan.FromDays(14);
            options.SlidingExpiration = true;
        });
        services.AddDistributedMemoryCache();
        services.AddSession();

        services.Configure<SettingsViewModel>(Configuration.GetSection("Settings"));
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseSession();
        app.UseAuthentication();

        var cookiePolicyOptions = new CookiePolicyOptions
        {
            MinimumSameSitePolicy = SameSiteMode.Strict,
        };

        app.UseCookiePolicy(cookiePolicyOptions);
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }
        app.UseStaticFiles();

    app.UseCookiePolicy();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }
}

一个临时修复,我直接发布到调试文件夹,我需要发布到禁用razor编译的netcoreapp2.2文件夹。 如果任何人有更简单的部署,这仍然不是将网站发布到控制台应用程序根目录的理想解决方案。

Program.Main ,在调用CreateWebHostBuilder之前将当前目录设置内容根路径

此外,本文可能是在Windows服务中托管ASP.NET Core网站的好方法:在Windows服务中托管ASP.NET Core

更多细节

当您使用app.UseStaticFiles()中间件时,应用程序希望在默认情况下wwwroot“Web Root”目录)中找到静态文件。

但要了解wwwroot ,应用程序必须首先知道其基本路径,即内容根路径

哪一个

确定ASP.NET Core开始搜索内容文件的位置,例如MVC视图

哪个

  • 需要设置在Windows服务运行( 或者从另一个程序中长时间运行的任务时产卵 )时

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM