Relative File Path in Blazor Server hosted on Site - c#

I have a blazor server app (.net 6) that worked fine locally. I needed to load a configuration file for the Ebay api, so it is a YAML file kept in the structure of my project.
On the server I can hard code access to it with the following path and the site works perfectly:
$"D:/home/site/wwwroot/Config/ebay-config.yaml"
However, when I try to get the exact same result using the relative path method for blazor server I am screwing it up somehow.
$"wwwroot/Config/ebay-config.yaml"
That was the first thing I found and tried. It gives me an error that it can't find $"D:/home/site/wwwroot/wwwroot/Config/ebay-config.yaml". Clearly the relative file path works to an extent, but for some reason wwwroot is put in there twice and it fails.
Using $"{env.WebRootPath}/Config/ebay-config.yaml" gave a similar result, with two wwwroots.
Can someone please tell me what the correct method to use to get to the wwwroot folder from the web server? It is currently hosted on Azure if that matters.
Thanks in advance.

Just in case anyone else comes looking...
Per the comment from Lex I went back and looked at it again. Using the file locationg like this works on both localhost and azure server.
$"Config/ebay-config.yaml"

Related

Azure function app wwwroot folder missing

I have an Azure Function app which is constantly returning 404 for all of the endpoints. The API configuration seems correct so the issue is with the function itself. It had been working fine until recently. The only thing that I have noticed which is strange is that when I compare the folder structure using Kudu, a working function app has a /home/site/wwwroot directory,
but my broken function app does not:
I think this has something to with my issue. I tried to manually create the folder and I get a 409 error.
The code is written in C# and deployments don't work using the azure pipeline as well as manually doing a deployment with
func azure functionapp publish <function app name> --dotnet
I noticed that the app setting WEBSITE_CONTENTSHARE was set to a value that was originally only on the staging slot, something like staging-asdf-fdsa, could that have caused the problem? I tried changing the value and restarted the function app but it didn't seem to make a difference
Does it seem like I am on the right path? Does anyone know why this folder would be gone? I'm wondering if an app setting that I had might have broken my function app when I was doing a swap between my staging slot and production slot.
If there's any additional info that would help please let me know and I will edit this question with it.
UPDATE
I was able to get it to properly create the wwwroot directory by toggling the WEBSITE_RUN_FROM_PACKAGE from 1 to 0, when I did my deployment, it switched it back to 1 and created the wwwroot directory.
The issue is that I still receive a 404 for a valid URL.
Found the root cause. I looked into the C# application that is running and I found that in Startup.cs, the api route suffix was being set. MS documentation is very confusing around this and the intention seems to have been to switch from /api to /api/<something>, and while it appears that worked for the function itself internally, the azure function app was not aware of this change in route, so it kept trying to serve the request at /api. removing this line fixed the issue. I believe that the likely solution in the end if we want to change the route suffix is to update both host.json and the value in Startup.cs, but that's another issue for another SO ticket (hopefully not)

asp.net and c# with javascript

((LinkButton)e.Item.FindControl("my_label_name"))
.Attributes.Add("onclick","javascript:myJavaScriptFunction('"
+ data1_from_db + "','"+data2_from_db+"')");
I wrote this code (this code is in my default.aspx.cs) and this worked successfully at localhost but at server didn't work. And gives no error about working. Just it doesn't work. If any incomprehensible places have, please, ask me.
My guess would be that the call to the myJavaScriptFunction is probably failing. Your javascript file (.js) is probably either not included or not marked as content within the project and would not get copied as part of the server installation.
EDIT: Based on your comments to my answer, it seems that your javascript (.js file) is being included and called on your server. If that is true, it is possible to debug your javascript by either using (IE Developer Tools - F12) or something like FireBug in FireFox to see what's happening within your javascript.
Is that part of code an update you made to your page that was already on the server? If so, just try clearing your cache. This just might work.
Double check that there is data on the server side. Say for example you are pulling info from localhost and everything is fine because that data does exist in the database on your local machine, however this data may not exist on the server's database. Double check to make sure it does and that the table name / info matches and so does the connection string.

FTP upload in .net — not getting correct file path in some browsers

I'm building an application which involves writing some fields to a database, along with uploading some files from the end user to an FTP site. The file upload works fine... in IE. In Firefox and Chrome, I get an error that it can't find the file (running it in localhost at this point, haven't moved it to a dev or production environment yet).
I have tried getting the file via:
Server.MapPath(FileUpload1.PostedFile.Filename)
... which points to the folder the application is residing in.
And also:
Path.GetFullPath(FileUpload1.PostedFile.Filename)
... which points to c://Programs (x86)/... ...
I can get a file to upload properly if I get it from either folder, but nothing from anywhere else.
Any ideas on how to make this point to the right place? Or, will it actually work properly once it resides in a server environment?
Thanks in advance!
FileUpload.PostedFile.Filename works differently in each browser. in Firefox and Chrome it won't include the full path - just the file name. It depends on your customer's browser.
FileUpload.PostedFile.FileName
This actually gives you path of the uploaded file.
But in all the newer browsers (FF 3.6 series, Chrome, IE7+) this feature has been disabled due to security reasons. Any website should not need path of a file stored in client's systems because that gives the directory structure and may expose other important things to website owner.
So in your case, the above code returned only the file name.
you can check this link, it may help you Fileupload control - fullpath issue

Process.Start not working on UAT for opening file

this works on dev as usual when I put it on UAT the code does something different. A PDF is saved on the server, the class then opens it up using the url of that file.
The URL works fine if I paste it into a browser, but doesn't work from the code.
Here's my code:
Process.Start(openPath);
openPath will look like: "http://www.cbm360.net/test/temp/CBM360Report_1093750.pdf"
The file is there on the server, but it just won't open in the code.
The code is inside a web method called using AJAX, if that makes any difference, I'm not sure.
Does anyone have any suggestions as to why this isn't working?
The Exception is:
System.ComponentModel.Win32Exception: The system cannot find the file specified
Thanks!
Instead of pasting the URL into a browser (I assume this is on the server), try it directly from the command line on the server. Does it work now? Process.Start is not the same as navigating to a URL in the browser, it is more akin to running the URL from the command line. I am not sure what you are trying to achieve. Normally we would use a web request to get a pdf.

How to retrieve on which site the web application is currently running?

I am currently running a WCF service on an AppFabric server and my application needs to load a the web.config file dynamically to retrieve custom configuration sections.
On my development machine I can just load the configuration like this:
WebConfigurationManager.OpenMappedWebConfiguration(webMappedFile, virtualPath);
But on the test machine (AppFabric server) I am getting an exception and it seems that I need to specify a third parameter which is actually the site the web application is running on:
WebConfigurationManager.OpenMappedWebConfiguration(webMappedFile, virtualPath, "MySite");
So I tried to hard code it and it worked. Anyway this is not acceptable, so I need to dynamically provide the site to the WebConfigurationManager because I do not on which site the service will be running in the future. Do anybody knows how to achieve that?
Thanks.
If you are running this code as part of handling a request you could use:
Request.ServerVariables("server_name")
see: http://msdn.microsoft.com/en-us/library/ms525396(VS.90).aspx
Edit based on your comment
The parameter that you need is the Site Name, not the machine name, your code be running on many machines. If the code is running somewhere where it no longer knows that it is on a web site, then it is difficult for it to get the name of the web site that it is running on.
You then have two options:
Send the name as a parameter from a layer that has httpconext
Not sure if this will work: but you could try adding a reference to system.web to your project. It may compile, but you could get a null reference exception when you run it. Probably worth a try.
How about Server.MachineName

Categories

Resources