.net CORE如何使用特定路径添加JosonFile()?

时间:2017-08-24 16:57:47

标签: linux angular asp.net-core

我刚将我的.net核心项目部署到Ubuntu服务器,由Kestrel托管。在frontEnd我使用Angular 4和cli,它通过API调用显示一些要编辑,删除等的数据。后端只是进行CRUD操作的API。 Backend和Frontend都由Kestrell主持。部署成功,如果我更改AddJsonFile()属性,应用程序正在按预期工作除外。 我很困惑AddJsonFile()属性为它设置常量路径。

这是program.cs:

    public static void Main(string[] args)
    {

        var jsonFilePath = "/wwwroot/backend/publish/hosting.json";
        var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile(jsonFilePath, optional: false)
        //.AddJsonFile("hosting.json", optional: false)  -- working

        .AddCommandLine(args)

        .Build();

        var host = new WebHostBuilder()
        //.UseUrls("http://*:8088", "http://0.0.0.0:8088")

        .UseEnvironment("Development")
        .UseConfiguration(config)
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())

        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

        host.Run();

        //BuildWebHost(args).Run();

}

这是Startup.Cs上的Configure方法:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
ILoggerFactory loggerFactory)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();app.UseMvc();
        app.UseCors("MyPolicy");


        //Kestrel - static files hosting
        //Default : under wwwroot folder
        app.UseDefaultFiles();
        app.UseStaticFiles();

    }

在ubuntu上,所有项目文件都在 wwwroot / backend / publish 文件夹下。  前端文件夹位于发布文件夹中。 hosting.json也在项目的根文件夹中。在hosting.json里面我设置了端口:

{
  "server.urls": "http://*:8088;http://*:8089"
}

在ubuntu服务器8088和8089是内部端口时,它们会转发到9388和9389外部端口。所以我可以从外部网络访问172.17.2.43:9389和172.17.2.43:9388的网站

使用此设置:.AddJsonFile("hosting.json", optional: false)后端和前端运行时没有任何错误。但每次我用putty连接到服务器我必须通过键入cd / path然后dotnet myapp.dll等来发布文件夹。为了节省我的时间,我已经在ubuntu的根文件夹上创建了bash脚本来运行项目:

dotnet backend/publish/myapp.dll 

当我运行bash脚本时,dotnet给出一个错误说:找不到hosting.json文件,所以我改为

.AddJsonFile(jsonFilePath, optional: false)

当我再次运行bash脚本时,看起来app正在运行,说:

Now listening on: http://[::]:8088
Now listening on: http://[::]:8089
Application started. 

当我与邮递员进行api通话时,它有效。 但该网站给出了错误:

GET http://172.17.2.43:9389/ 404 (Not Found)
contentscript.js:88 Uncaught Error: uBlock Origin: aborting content scripts for http://172.17.2.43:9389/
    at contentscript.js:88
(anonymous) @ contentscript.js:88
popup.js:7 Uncaught TypeError: Cannot read property 'getSelected' of undefined
    at popup.js:7

我不知道是什么问题,app看起来正在运行,api调用给出响应,但无法访问该网站。

更新(解决)

我怀疑你是否在linux上运行项目dll文件夹之外的dotnet [Path]/Projectname.dll命令(在我的项目上它是发布的),它不会托管静态文件

例如:

project folder on linux : (root)/wwwroot/backend/publish
wwwroot of project :     (root)/wwwroot/backend/publish/wwwroot

当您在根文件夹中键入dotnet /wwwroot/backend/publish/MyProject.dll

Kestrel只是托管后端dll文件,它无法找到静态文件所以当你去网站时它会给出404错误。要修复它,您需要在Main()方法初始化WebHostBuilder()类时添加带有正确路径参数的.UseWebRoot()

 .AddJsonFile("/wwwroot/backend/publish/ + "hosting.json", optional: false)
 .UseWebRoot("/wwwroot/backend/publish/wwwroot");

如果你不想输入硬编码的路径,你可以像这样使用:

var publishPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var webRoot =  publishPath + "/wwwroot"; //wwwroot/backend/publish/wwwroot

这是我的应用程序的主要方法,它按预期工作:

 public static void Main(string[] args)
    {

        var publishPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
        var webRoot =  publishPath + "/wwwroot"; ///wwwroot/backend/publish/wwwroot

        var config = new ConfigurationBuilder()
        .SetBasePath(publishPath)
        .AddJsonFile(publishPath + "/hosting.json", optional: false)
        //.AddJsonFile("hosting.json", optional: false)  -- working
        .AddCommandLine(args)
        .Build();


        var host = new WebHostBuilder()
        //.UseUrls("http://*:8088", "http://0.0.0.0:8088")

        .UseEnvironment("Development")
        .UseConfiguration(config)
        .UseKestrel()
        .UseContentRoot(publishPath)
        .UseWebRoot(webRoot)

        .UseIISIntegration()
        .UseStartup<Startup>()
        .Build();

        host.Run();

        //BuildWebHost(args).Run();

    }

0 个答案:

没有答案