Publish Net Core Web API in NAS Synology, web root path problem

170 views Asked by At

I am trying to use the Synology package ".NET 6.0 runtime" by hgy59 to create a small website. I am using the example of this post : Publish Net Core Web API in NAS Synology

I can't find the github or any repository where I can ask questions so here I am.

I am a bit of a noob regarding ASP.NET. I am usually developing in C# or VB but no web. But know I need it.

So, the question : How can I set the working directory to the wwwroot folder of the published app instead of '/usr/syno/synoman/webapi/ ?

Thanks

2

There are 2 answers

0
jérémy Courbat On BEST ANSWER

I have finally found a solution to my problem bellow so I post it there. It was indeed a problem with the root directory. To fix it : you need the following lines of codes

string basedir = AppDomain.CurrentDomain.BaseDirectory;

WebApplicationOptions options = new()
{
  ContentRootPath = basedir,
  Args= args,
  WebRootPath = Path.Combine(basedir, "wwwroot")
};

var builder = WebApplication.CreateBuilder(options);

Yes the rootpath need to be set and pass to the builder. I had tried to modify the root path AFTER the creation of the builder but it was ignored and i had given up. But this works!

1
Jason Pan On

You can use SetCurrentDirectory to set the working directory.

Usage:

var builder = WebApplication.CreateBuilder(args);

// make sure you have permission to access '/usr/syno/synoman/wwwroot/'
var pathToContentRoot = @"F:\\sites\\m3u8";
Directory.SetCurrentDirectory(pathToContentRoot);

Tips

When the application terminates, the working directory is restored to its original location (the directory where the process was started).