0
votes

In an ASP.Net Core webapplication pages are searched from certain paths. For example, the Index page in a Razor Page application is located in the folder "Pages" (by default), which is relative to the application root folder. The page can thus be accessed with the path "/Index".

However, I would want to access the pages by a true absolute path. Which means that the entire path would have to be specified to access the view. On Windows the complete path could be C:/Project/Pages/Index.cshtml. But ASP.Net Core can not handle this by default, becuase this path would be resolved to: /Pages/C:/Project/Pages/Index.cshtml (relative to the application root, which is wrong.

How can I change this behavior so that ASP.Net Core does not interfere in this manner? The reason I want to change this behavior is that on Linux when a path starts with "/", then it points to the root of the filesystem and not to the root of an application. I want the same behavior everywhere.

Note: I do not want to use the tilde character ("~") either.

Thanks!

1
So, are you trying to load views that are outwith the web application?Scriptworks
“access the pages by a true absolute path […] the complete path could be C:/Project/Pages/Index.cshtml” – That seems like a terrible idea. Why would you possibly want that? Application code should always be relative to itself. It shouldn’t depend on where the source code is located. Not to mention that it will very likely be a different location once you deploy to production.poke
@Scriptworks Not sure what you mean with "outwith", however, I'm just trying to load the views based on their true absolute path, regardless of where they reside. For example, in PHP I can load whatever file I want and from anywhere I want just by specifying the path to it.Acmion
@poke I agree, I just left out some small details for the sake of brevity. The complete path would never actually be hardcoded, but more in the style of: Application.RootPath + "/Pages/Index.cshtml". I want this because of the following situation: Some content is stored in static HTML and some in CSHTML. To load in HTML, I use File.ReadAllText(absolutePath), but to load in a partial view I have to do something like: Html.RenderPartial(GetAppRelativePath(absolutePath0)). The paths are determined at runtime as absolute paths (or at least easily convertible to such by joining the app root).Acmion
If you want runtime compilation for your Razor views then just enable that. But any approach where you compare ASP.NET Core with things like PHP will simply not work. These are fundamentally different and incompatible application models.poke

1 Answers

0
votes

It is really difficult because when deploying the application are *.dll files and then it will not find the file in that path.

I recommend using the relative path "~"