Blazor Server Path Issues - c#

I hope someone can help I'm still pretty new to Blazor and I've been struggling with this problem all day.
I had a Blazor Server app that ran fine on my local machine and also fine on my deployment server (Ubuntu/with Apache proxy). It ran fine for months until I needed to move it from the root of the server to a subfolder e.g. https://myserver/myapp. That's when my nightmare began. Even though all the content paths are relative for css, images etc they were all trying to load from the root / of the website after I moved it.
There's several ways I have found for setting the path of the app.
app.UsePathBase("/myapp")
app.UseStaticFiles("/myapp")
Setting these seem to be completely ignored when deployed on the server for some reason so I had to look for other options. I then found that you can simply set the path in the tag in _Layout.cshtml:
<base href="/myapp/" />
This worked great... all the content loaded as intended and I thought it was fixed until I realised none of the dynamic js elements was working.
I tracked that down to a incorrect path in the script tag "/_framework/blazor.server.js" so I corrected the script tag to be "_framework/blazor.server.js".
Now its trying to load the script from https://myserver/myapp/_framework/blazor.server.js - the problem is that path does not exist! Yet somehow it works fine on my local dev environment.
I've tried many things... even copying the _framework folder from my local environment to wwwroot manually - which resulted in some strange errors and did not seem like a good idea.
I've also tried using ~/ as the base href which works locally but breaks on the server where it's just trying to serve everything from the root folder again.
I don't understand why things are working very differently in the development environment and the published server build. I also can't seem to change where that _framework folder is located and so can't load the necessary script files.
Is it something you do with the app.MapBlazorHub() command?

Adding the code below to your _layout file configures the signalR hub to use an absolute path instead of a relative path:
<script src="~/_framework/blazor.server.js" autostart="false"></script>
<script>
Blazor.start({
configureSignalR: function (builder) {
builder.withUrl("/_blazor");
}
});
</script>
Now, you can use your absolute path for it.

In the end I worked around this by placing my app on it's own subdomain myapp.myserver.com. It doesn't really solve the original problem but Blazor Server apps just seem to work with much less hassle when they are in the located in the root path.

Related

Unable to determine if a file is on a web server because the various methods of determining the directory do not work

I am developing an application in asp.net, vs2015 using c# and the development environment is a Win10Pro machine. I can use any of the various methods to obtain the working directory and see if a particular file exists on the dev pc, but not on the Web Server. I have tried the methods laid out on:
Get current application physical path within Application_Start
All work on the Dev PC, but when used on the Web Server it will not return the working directory. The Server is a 2016 Data server using IIS10. The issue is that the web site I am putting together work fine, except to display GrapeCity ActiveReports reports AR15. The web page containing their web viewer opens just fine and is looking for a report file (MyReport.rdlx). The global.aspx file is pointing to the root directory but when the web viewer opens up, it says File Not Found. I have absolutely no idea and tech support is not sure. Is this an IIS issue that is preventing the code to locate and verify the file is there? Any direction would be much appreciated. This has been very frustrating and time consuming.
AppDomain.CurrentDomain.BaseDirectory does not work, HttpRuntime.AppDomainAppPath does not as well as all the others. The request comes back blank.
string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() +"SPU01_Dates.rdlx";
if (File.Exists(filename))
{
Response.Write("YES");
}
else
{
Response.Write("NO");
Response.Write("</br");
Response.Write(filename);
}
All this just returns nothing.
Thanks.
Try this code
if (File.Exists(Server.MapPath(filename)))
Check if a file exists on the server
In my test, it returned YES and worked well. Did you put "SPU01_Dates.rdlx" file in root folder?
In the development environment, it returned YES, and when I deployed it to IIS, it returned NO. I found that during the deployment process, the rdlx file was not deployed with the project, so I recreated one in the deployed folder, and it returned YES.
The test proves that AppDomain.CurrentDomain.BaseDirectory is the most accurate way to get the file path. When you test this code in IIS, does it return NO or empty? Returning empty means that this piece of code has not been executed.

Failed to load resources

I have application which I deploy to Azure and suddenly I catch one error which wasnt in my local machine when I tested application.
Failed to load resource: the server responded with a status of 404 (Not Found)
SO when I tested application in my local machine everything works perfect without any errors, and when I move to test application live on server many option doesnt work, and in console manager I get this kind of error.
Any help, what can be problem here ?
404 is resource not found error. Most probable reasons are your files are in “../“ folder and you are trying to access file in “../..” folder.
I would suggest using URLs like /Folder/subfolder instead of relative URLs like “../parentfolder/subfolder”..
Also, its good to use “~”..
More on paths here:
https://msdn.microsoft.com/en-us/library/ms178116.aspx

Failed to load resource, error 404, running with IIS Local

Good morning, I developed a mvc 5 application, during development I used IIS Express, all the files there loaded perfectly, when I uploaded to my IIS Local (7) still on my machine to test, I realized that several files are not loaded.
Failed to load resource: the server responded with a status of 404 (Not Found)
glyphicons-halflings-regular.woff
When checking I identified that the file is not loaded because in fact, the path that tries to perform the GET is incorrect. It tries to get get on the path:
http://localhost/content/fonts/glyphicons-halflings-regular.woff
But the correct file would be:
http://localhost/aprovacoes/content/fonts/glyphicons-halflings-regular.woff
In the get, the project name is missing, due to this it is not loaded.
To solve the problem of this particular source, I needed to edit the bootstrap.min.css file and change the url from url:
url(/content/fonts/glyphicons-halflings-regular.woff)
for
url(aprovacoes/content/fonts/glyphicons-halflings-regular.woff)
However in every project several files are not loaded, and to solve I'm editing the css file by file, would not there be another way to map this correct path in all css files?
Thank you very much in advance
Use a resource-relative path. For example, if you have a file structure like:
+ bootstrap.css
- fonts
+ glyphicons-halflings-regular.woff
Then, use the URL: fonts/glyphicons-halflings-regular.woff. Or if it's something like:
- css
+ bootstrap.css
- fonts
+ glyphicons-halflings-regular.woff
Then, you'd use: ../fonts/glyphicons-halflings-regular.woff.
That way, it won't matter how or where the site is deployed, as long as the relative path between the files remains consistent.

ASP physical and virtual path

This is the first time I make an asp site. This line of code is working fine on my pc but obviously to make it working on the production server I need to change the reference.
DirectoryInfo dir = new DirectoryInfo(#"C:\Users\Pink\Documents\Visual Studio 2012\Projects\ManagDoc_Framework\Test1_managDoc\Test1_managDoc\Allegati\" + recordIDcreateDir);
I have tried many sort of path combination but I am not getting it right.
I would like to find a solution that makes the code working on both pc, during development, and hosting server without having to change the code.
How should i write the path? Some help will be appreciated.
Use Server.MapPath method :
The MapPath method maps the specified relative or virtual path to the
corresponding physical directory on the server.
Additional details on W3schools.com, tutorial I followed, and where I learnt the existence of the above method.

Server Root and MapPath()

I have a file structure set up like this:
ServerRoot
applicationRoot
filePage.aspx
files
chart.png
My application page called filePage.aspx uses another app to custom build charts. I need them saved in files folder. This is how our client's production server is set up and I cannot change this.
I do a _page.Server.MapPath("/files") but it gives me a InvalidOperationException and states Failed to map the path '/files'.
UPDATE:
So it has to be set up this way MapPath("/"). My local asp.net server can't handle the MapPath that way, but our IIS development box has no problem with it and it works fine. Interesting.
How do I get it to save to files?
I believe it's a security violation to go outside the directory structure of the virtual directory in asp.net 2.0 and up. You'll need to make a virtual directory to the directory and use that.
Use
Server.MapPath("~/files")
The ~ represents the root of the web application so the folder returned will be correct no matter which subdirectory you are in.
Try
_page.Server.MapPath("files")
_page.Server.MapPath() will attempt to map from the root of the application (NOT the root of the server).
Try _page.Server.MapPath("../files").
EDIT
You may run into security issues when trying to map outside of your application root. If that is the case, you can do something like this:
Server.MapPath("~").Substring(0, Server.MapPath("~").IndexOf(VirtualPathUtility.ToAbsolute("~").Replace("/", "\"))) + "\files"
This looks rather complex, but essentially says "map my application, then remove the application root from the end and add '\files' instead".

Categories