C# 从asp.net核心控制台应用程序运行网站
我有一个控制台应用程序,这将是一个服务,它有一个网站来配置服务 我已经建立了我的网站,当我运行该项目时,它运行良好 我在控制台应用程序中引用了我的web项目,并在其中使用以下代码启动网站C# 从asp.net核心控制台应用程序运行网站,c#,asp.net-mvc,asp.net-core,C#,Asp.net Mvc,Asp.net Core,我有一个控制台应用程序,这将是一个服务,它有一个网站来配置服务 我已经建立了我的网站,当我运行该项目时,它运行良好 我在控制台应用程序中引用了我的web项目,并在其中使用以下代码启动网站 Task.Factory.StartNew(new Action(() => Website.Program.Main(null)), TaskCreationOptions.LongRunning); 我在website程序类中的代码是 public class Program {
Task.Factory.StartNew(new Action(() => Website.Program.Main(null)), TaskCreationOptions.LongRunning);
我在website程序类中的代码是
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");
}
公共类程序
{
公共静态void Main(字符串[]args)
{
CreateWebHostBuilder(args.Build().Run();
}
公共静态IWebHostBuilder CreateWebHostBuilder(字符串[]args)=>
WebHost.CreateDefaultBuilder(args)
.UseStartup()
.useURL(“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?}");
});
}
}
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
配置(选项=>
{
//此lambda确定给定请求是否需要非必要cookie的用户同意。
options.checkApprovered=context=>true;
options.MinimumSameSitePolicy=SameSiteMode.None;
});
services.AddAuthentication(opt=>
{
opt.DefaultChallengeScheme=CookieAuthenticationDefaults.AuthenticationScheme;
opt.defaultsignnscheme=CookieAuthenticationDefaults.AuthenticationScheme;
opt.DefaultAuthenticateScheme=CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme,选项=>
{
options.Cookie.Name=“TmiginScheme”;
options.LoginPath=“/Account/Login”;
options.LogoutPath=“/Account/Logout”;
options.AccessDeniedPath=“/Home/AccessDenied”;
options.ExpireTimeSpan=TimeSpan.FromDays(14);
options.SlidingExpiration=true;
});
AddDistributedMemoryCache();
services.AddSession();
services.Configure(Configuration.GetSection(“设置”));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
app.UseSession();
app.UseAuthentication();
var cookiePolicyOptions=新cookiePolicyOptions
{
MinimumSameSitePolicy=SameSiteMode.Strict,
};
应用程序UseCookiePolicy(cookiePolicyOptions);
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
}
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(路由=>
{
routes.MapRoute(
名称:“默认”,
模板:“{controller=Home}/{action=Index}/{id?}”);
});
}
}
临时修复,我直接发布到调试文件夹,我需要发布到netcoreapp2.2文件夹,禁用razor编译。如果任何人有更简单的部署,这仍然不是将网站发布到控制台应用程序根目录中的理想解决方案。在Program.Main
中,与调用CreateWebHostBuilder
之前一样
此外,本文可能是在Windows服务中托管ASP.NET核心网站的好方法:
更多细节
使用app.UseStaticFiles()
中间件时,应用程序希望在wwwroot
(目录)中找到静态文件
但是要了解wwwroot
,应用程序必须首先知道它的基本路径
哪个
确定ASP.NET Core开始搜索内容文件(如MVC视图)的位置
哪一个
- 在Windows服务中运行时(或从另一个程序作为长时间运行的任务生成时)
Startup.cs
文件的内容吗?我想要一个windows服务,我还添加了我的Startup.cs代码