简体   繁体   English

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

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

I have a console application that will be a service, it has a website to configure the service. 我有一个控制台应用程序,它将是一个服务,它有一个网站来配置服务。

I have built my website and when i run that project it works fine. 我已经建立了我的网站,当我运行该项目时它工作正常。

I referenced my web project in the console application and inside there i use the following code to start the website. 我在控制台应用程序中引用了我的web项目,在那里我使用以下代码来启动网站。

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

my code in the website program class is 我在网站程序类中的代码是

    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");              
}

The issue i have is the website now cannot find the css files and javascript files in wwwRoot, i tried publishing the application to the debug folder of the console application but this did not work. 我的问题是网站现在无法在wwwRoot中找到css文件和javascript文件,我尝试将应用程序发布到控制台应用程序的调试文件夹,但这不起作用。 if i don't publish there is only dll files in the debug folder of the console application if i do publish all the files are there but the views cannot find them. 如果我不发布,控制台应用程序的调试文件夹中只有dll文件,如果我发布所有文件,但视图无法找到它们。

I am using ~ in the the views to find files. 我在视图中使用〜来查找文件。

Ideally I want a service that can be installed host its own website and API without it showing in the local computers IIS or need to enable IIS through windows features. 理想情况下,我想要一个可以安装自己的网站和API的服务,而不会在本地计算机IIS中显示或需要通过Windows功能启用IIS。

edit Startup.cs 编辑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?}");
        });
    }
}

A temporary fix, I was publishing to debug folder direct, I needed to publish to netcoreapp2.2 folder with razor compile disabled. 一个临时修复,我直接发布到调试文件夹,我需要发布到禁用razor编译的netcoreapp2.2文件夹。 This is still not an ideal solution to publish the website into the root of the console application if anyone has a more simple deployment. 如果任何人有更简单的部署,这仍然不是将网站发布到控制台应用程序根目录的理想解决方案。

Within Program.Main , set the current directory as the content root path prior to the call to CreateWebHostBuilder . Program.Main ,在调用CreateWebHostBuilder之前将当前目录设置内容根路径

Also, this article could be a good approach for hosting your ASP.NET Core website in a Windows service: Host ASP.NET Core in a Windows Service 此外,本文可能是在Windows服务中托管ASP.NET Core网站的好方法:在Windows服务中托管ASP.NET Core

More Details 更多细节

When you use the app.UseStaticFiles() middleware, the application expects to find static files within wwwroot (the "Web Root" directory) by default . 当您使用app.UseStaticFiles()中间件时,应用程序希望在默认情况下wwwroot“Web Root”目录)中找到静态文件。

But to know about wwwroot , the application must first know its base path, the content root path 但要了解wwwroot ,应用程序必须首先知道其基本路径,即内容根路径

which 哪一个

determines where ASP.NET Core begins searching for content files, such as MVC views 确定ASP.NET Core开始搜索内容文件的位置,例如MVC视图

and which 哪个

  • needs to be set when running in a Windows service ( or when spawning as a long-running task from another program ) 需要设置在Windows服务运行( 或者从另一个程序中长时间运行的任务时产卵 )时

暂无
暂无

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

相关问题 通过控制台应用程序在ASP.Net Forms授权网站上进行身份验证 - Authenticate on an ASP.Net Forms Authorization website from a console app 从控制台应用程序启动ASP.NET Core 1应用程序 - Launching ASP.NET Core 1 app from console application 用于登录ASP.NET网站的控制台应用程序 - Console app to login to ASP.NET website 使用Visual Studio for Mac部署asp.net核心网站或控制台应用程序 - Deploy asp.net core website or console app using Visual Studio for Mac ASP.NET 核心 3.1 MVC 下载文件直到 IIS 应用程序池回收或从本地主机运行网站 - ASP.NET Core 3.1 MVC download file not found until IIS app pool recycle or run website from localhost 如何使服务器端控制台应用程序在ASP.net网站(IIS7)上运行? - How to get server-side console app to run on ASP.net website(IIS7)? 在 asp.net 核心控制台应用程序中使用 log4net 进行日志记录 - Logging with log4net in asp.net core console app 为什么从另一个 .NET Core 应用程序运行时 ASP.NET Core 6 未发现控制器 - Why is ASP.NET Core 6 not discovering controllers when run from another .NET Core app 在 .NET 控制台客户端应用程序中使用 ASP.NET Core SignalR 从 Azure SignalR 接收消息 - Using ASP.NET Core SignalR in .NET Console Client App to Receive Messages from Azure SignalR 在控制台应用程序中重用来自我的 ASP.NET Core MVC 应用程序的代码:如何进行数据库连接 - Reusing code from my ASP.NET Core MVC app in console app : how to do database connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM