How to enforce Azure Virtual Application base path? - asp.net-core

I have an Azure App Service which has two virtual applications (Asp.Net Core Blazor apps, if it's important) hosted within it. The virtual application URLS are something along the lines of
https://site.azurewebsites.net/site1
https://site.azurewebsites.net/site2
I have noticed that some of the internal routing seems to not be taking the virtual application path into account. For example, a link that takes the site to the "/" route navigates to https://site.azurewebsites.net instead of the appropriate "base" virtual application path. Is there some way of either handling this in my application or within the azure configuration?
Thank you for any assistance!

Adding to Hassan's suggestions, on App Service, Each app has the default root path (/) mapped to D:\home\site\wwwroot, where your code is deployed by default. If your app root is in a different folder, or if your repository has more than one application ( as in your case), you can edit or add virtual applications and directories here.
To configure virtual applications and directories, specify each virtual directory and its corresponding physical path relative to the website root (D:\home). Optionally, you can select the Application checkbox to mark a virtual directory as an application.
From Azure Portal > Navigate to your WebApp -> Settings (Configuration)-> Path Mapping> Virtual applications and directories.

if you browse via console Kudu console in your app service blade you will notice a directory, under wwwroot is the root directory which by default your application routes to. This you can change from App Settings blade option. Also in this directory you will find you can create other folders aka virtual applications which will work the way you want it to.

Related

Blazor Server App routing 404 - File or directory not found

I have created a blazor server app. it is working on my machine when it is running using localhost. There is a folder configured in the IIS for this project.
I have published the project to a folder and then copied/paste the dll files to the configured folder in the IIS.
The main page (index page) is running perfect , but when I tried to click to move to another page, I got the error "404 - File or directory not found".
Note :- when I browse the project from the IIS , the name of the project has been replaced from localhost to abcdef.com.
Could anyone help me to correct the routing problem ?
I think its related to your page definition of your Balzor components. "/" means the root folder of your website. If you want to add this application to your existing website, you probably upload it in a new folder in your host. For example https yourwebsite/app. In this scenario your new application is not root of your website anymore. So your page address should be something like this:
#page "/app/address".

How to publish WCF service on IIS 7

I have developed and published (via the "publish" feature in VS 2013) a simple WCF web service on IIS on my local machine.
The physical deploy path is the folder : c:\inetpub\wwwroot\MyWCF_Service_On_IIS\
I saw on IIS that the virtual folder has been created under "default Web site" (with the same name : MyWCF_Service_On_IIS). The default web site listens on port 80.
Why there is no way to access the service ? whatever combination of paths and urls fails.
I tried :
localhost/MyWCF_Service_On_IIS/Service1.svc (Service1.svc is the .svc file hosting the only endpoint);
localhost/Service1.svc
127.0.0.1/MyWCF_Service_On_IIS/Service1.svc
127.0.0.1/Service1.svc
localhost
127.0.0.1
Even if I don't expose metadata endpoints I expect to see a least a help page about the service and how to get the metadata themselves.
But I only get errors
Whatever attempt I do fails miserably.
I would like to know, once and for all, what is the rule IIS uses to map virtual folders to physical folders.
We are supposed to directly deploy all related files/file directories to the root folder of the website by using FileSystem mode.
C:\inetpub\wwwroot
Moreover, we need to enable windows features to handle the SVC extension for supporting WCF service.
There is no need to use a virtual folder in that case.
Here is the content of the wwwroot folder on my side.
bin folder
PrecompiledApp.config
Service.svc
Web.config
The service URL is http://localhost/service.svc
If we specify the URL by using a virtual path. We should convert the virtual path to the application.
Then the service URL is http://localhost/MyWCF/Service1.svc.
Feel free to let me know if the problem still exists.

Get application virtual base path in aspnet core

I know that is located in HttpContext.Request.PathBase, however I need it to configure my cookie before I have any HttpContext (in Startup.cs).
My problem:
When devops configure application they have to set path twice. Once in IIS application (so hosting knows what should be served) and in my appsettings.json (so application knows where to set cookies - multiple instances can work on server). I would like to configure it once in IIS, and have config passed to my application.
There is a little confustion here that should be clarified.
You want to know application virtual path at application startup. However application virtual path is a concept of the hosting and specific request rather than underlying application. Hosting service uses this virtual path to map incoming url to application root. In IIS you can map multiple virtual paths to the same physical directory, e.g. /myApp1 and /myApp2 will point to the same application. Which of these paths do you want to get at application startup?
That's actually the reason why IHostingEnvironment interface does not provide any property for getting application virtual path. Another thing when application processes the request. In this case specific URL is requested and ASP.NET could provide requested virtual path in HttpContext.Request.PathBase.
You should reconsider your use case and check whether you actually need application virtual path for cookie configuration. It could be the XY problem. If you still have doubts regarding cookie configuration, please formulate it as a new question with specific details for your scenario.
You can check the environment variable ASPNETCORE_APPL_PATH. This is the variable AspNetCoreModule provides so that PathBase can be set correctly. See https://github.com/aspnet/IISIntegration/blob/df88e322cc5e52db3dbce4060d5bc7db88edb8e4/src/Microsoft.AspNetCore.Server.IISIntegration/WebHostBuilderIISExtensions.cs#L19
Just to add you can use #Url.Content("") in Javascript to get virtual directory base path.
<script src="#Url.Content("~/app/myapp.js")" type="text/javascript"></script>

Virtual Directory to navigate to http://localhost/MyWebsite instead of http://localhost:8080

I have deployed an ASP.NET MVC 4 application to a new site I have created in IIS 7.5, which I have bound to port 8080. I can reach it by navigating to http://localhost:8080, but I want to reach it via http://localhost/MyWebsite.
I have added a Virtual Directory under my website, which points to "C:\inetput\wwwroot\MyWebsite\". However, when I navigate to http://localhost/MyWebsite, I am presented with a configuration error:
"It is an error to use a section registered as
allowDefinition='MachineToApplication' beyond application level. This
error can be caused by a virtual directory not being configured as an
application in IIS."
Here is what my IIS hierarchy looks like (this is a demo since I have no internet access on the server I am working on).
I have two questions:
Why am I getting this error?
Is this the best way to go about achieving what I want? It seems messy to have the list of files and folders underneath the website and then again underneath the Virtual Directory. If there is better practice then please tell!
Virtual Directories cannot execute scripts, reason why you are getting that error. You need to make your MyWebsite folder an Application. Also, you don't necessarily have to create a separate website for your website, you can use the Default Web Site and create an application MyWebsite in there (it might be less confusing maybe?).

ImageResizer not working with virtual directories

I am using the ImageResizer successfully on other areas of my site where the images are being served from a folder within the application (both .jpg?width=100 & .jpg.ashx?width=100 work), but when I try images from a virtual directory located on the local machine, I can only get resizing to work if I specify the image page using the "jpg.ashx?width=100" method.
The site's application pool is using NetworkService identity and the virtual folder has pass-through authentication enabled. I've granted Modify permissions to NetworkService on the virtual folder too hoping that would help.
I am using IIS 7.5 in Classic Mode and the resizer diagnostics page doesn't show any issues.
Does anyone have ideas on why this may be happening? Any help would be appreciated.
Thanks!
Classic Mode requires the .ashx syntax, since ASP.NET doesn't see static file requests.
If you're seeing different behavior within the same site on the same host, then your virtual folder is probably NOT a virtual folder, it's likely a sub-application.