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