How can I get the path of a cs file in aspx?
Here's the issue: I've created a property class in a property.cs file which is used in an EPiServer admin module. Server.MapPath() will hence return the path of the executing file, which is in a totally different place than my code. I need to get the path of the property.cs file (from inside the property.cs file) in order to dynamically set some relative paths to css and js files. How can this be done?
I hence want to be able to include .js and .css files in this cs file, all files located in the same directory, but the cs file is accessed from the EPiServer UI.
I would highly recommend not doing what you are trying to do. You'll be constructing a brittle dependency on files that should not even by deployed with your project.
If you have web classes that rely on resources like javascript and css, you should use the ClientScriptManager (or ScriptManager for ajax apps) to register the script files onto the page, and the scripts and css should be deployed into their own regular web directory.
If deployment location is a problem, and you're creating some kind of reusable, redistributable module, then I would recommend that you embed the .js and .css files as WebResources in your assembly, and use the script manager to register the scripts to the page that way, with ClientScriptManager.RegisterScriptResource().
I think you should go with including your CSS, JS and other static files as embedded resources.
That will include the files inside the DLL, which makes deployment easier. You can then set up a HTTP handler which will serve you the contents of the embedded files - or use the aforementioned RegisterScriptResource() method.
By embedded the files you don't have to know any file paths.
ASP.NET is compiled, you should never need to read settings from source files at runtime, you should read from configuration files (*.config), if they need to be dynamic, these too can be injected during the page lifecycle via a variety of methods.
Related
I want to deploy my website with precompiled views, for performance reasons. So I have configured UseMerge and PrecompileBeforePublish.
This is part of my publishing profile:
<PrecompileBeforePublish>True</PrecompileBeforePublish>
<EnableUpdateable>False</EnableUpdateable>
<UseMerge>True</UseMerge>
<SingleAssemblyName>Conwell.Administration.Views</SingleAssemblyName>
<DeleteAppCodeCompiledFiles>True</DeleteAppCodeCompiledFiles>
In the UI this is reflected:
My Conwell.Administration.Views.dll gets successfull created. However after publishing, I have for each View an additional .precompiled file in my bin folder:
The content reads as following:
<?xml version="1.0" encoding="utf-8"?>
<preserve resultType="2" virtualPath="/Areas/Bookings/Views/SepaDebits/Detail.cshtml" hash="fffffffff9d57aef" filehash="1737cd4f2d0e" flags="110000" assembly="Conwell.Administration.Views" type="ASP._Page_Areas_Bookings_Views_SepaDebits_Detail_cshtml">
<filedeps>
<filedep name="/Areas/Bookings/Views/SepaDebits/Detail.cshtml" />
</filedeps>
</preserve>
I tried to simple delete them, but then the website shows only a blank page. I don't like that many *.compiled files. They add up to more than thousand.
For what are they used? ViewEngine? Is it somehow possible to disable them? Maybe a custom ViewEngine?
I have only found this thread so far, but it doesn't give any pointers.
For executable files in an ASP.NET Web application, the compiler
assemblies and files with the .compiled file name extension. The
assembly name is generated by the compiler. The .compiled file does
not contain executable code. Instead, it contains only the information
that ASP.NET needs to find the appropriate assembly.
After the precompiled application is deployed, ASP.NET uses the
assemblies in the Bin folder to process requests. The precompilation
output includes .aspx or .asmx files as placeholders for pages. The
placeholder files contain no code. They exist only to provide a way to
invoke ASP.NET for a specific page request and so that file
permissions can be set to restrict access to the pages.
Source: https://msdn.microsoft.com/en-us/library/e22s60h9(v=vs.85).aspx
You will have to remove the pre-compile steps to remove them, or deploy the source code alone. Use build server to compile real time.
I would just leave them. They cause no harm.
I store my language resource files as JSON and need to load and deserialize them using Newtonsoft.Json package.
It wouldn't make much sense to put these files in the wwwroot folder since they are not to be used or opened directly by the client.
I tried to load them using File.LoadAllText() but it tries to find those files in the wwwroot and won't look outside of that.
Is there anyway I can load them?
You can access any files your environment allows even though the working directory by default is set to wwwroot. It makes sense to store data that is not accessed directly by the client to other directories than wwwroot.
You can use the services at PlatformServices.Default.Application to get the path to the approot folder via ApplicationBasePath. This way you can be sure you are accessing the correct files without relying on working directory and relative paths.
To use this you have to add a reference to Microsoft.Extensions.PlatformAbstractions.
I have a situation, where html files are embedded as resource files in console WebApp and are sent to another app, that hosts them.
I managed to create file change procedure, so that the WebApp is reset and will resend resources, when I edit my html files.
The problem is, embedded files doesn't change.
Is there a way that I can update embedded files at runtime?
No, but you could save the updated files to a folder, and use those files.
When the applications starts, it first checks if the folder with the new files exist. If they do, the application will use them else the application will fall back on the embedded files.
In Brief:
In an ASP.net website with a code-behind, at what point are the *.cs files compiled?
Context:
A colleague who has since left, deployed a website with a .cs code-behind to a shared server. I have made a small change to a .cs file, which I should expect to reflect on one of the pages but it has not yet appeared. I have restarted the application pool, however I am loathe to reset IIS on the server as there are couple of other teams' apps which might be be in use on the same server.
This applies to Web Application projects as opposed to Web Site projects, which are CodeFile by default, and don't allow changing the build action...
In ASP.NET Web Applications you have two methods of deploying your pages; CodeFile and CodeBehind. By default pages will always use CodeBehind but you can change this.
CodeBehind
CodeBehind compiles your .cs file into the .dll file in your bin folder at compile/build time, and then you deploy that to your web server. There is no need to deploy the .cs file to your web server. If you do, it will just sit there being unused.
To configure a page with CodeBehind, ensure that:
The page directive in your .aspx file has CodeBehind="your.aspx.cs"
The properties of the .cs and .designer.cs files in solution explorer have a build-action of compile.
CodeFile
This causes ASP.NET to compile the .cs file on-the-fly on the server. This means that your .cs file needs to be deployed to the web server. It also means that your .cs file will not be compiled at compile/build time and therefore not built into your .dll in the bin folder.
Key advantage
With CodeFile, You can make changes to the .cs file and deploy just that file to see the changes on your production web server. No need to re-deploy. No need to recycle the app pool. This can be very useful in a lot of situations.
To configure a page with CodeFile, ensure that all of the following are met:
The page directive in your .aspx file has CodeFile="your.aspx.cs"
The properties of the .cs file in solution explorer have a build-action of content
The properties of the .designer.cs file in solution explorer have a build-action of none.
Notes
Intellisense doesn't like working when pages are set up with
CodeFile (you can change to CodeBehind whilst coding and then change back for deployment, though).
If you change from CodeBehind to CodeFile, then always do a
rebuild and re-deploy (and vice versa). This is because when the page was CodeBehind,
the .cs was compiled into the .dll in the bin folder, and will
remain there when you change to CodeFile. The CodeFile will be
compiled on-the-fly and you will get the same code/classes defined in
the .dll and in the on-the-fly compiled code, which will lead to
runtime errors.
For the setup I use, the .cs files are compiled when building the project. This means it is the .dlls in the bin that need to change, not the .cs files directly.
The .aspx files can change at any time, but I think you need to rebuild the project in order for the code behind to take effect.
I have replaced singular .dlls before without any problem (though it's not good practice).
Apparently what you have done should work.
Check if Cacheing has been implemented.
Otherwise publish the code and deploy the dll, instead of .cs file. I would recommend to test in staging server before you go live.
Since all files in a web project are compiled into single assembly, then does this assembly maintain a directory structure? For example, if a file in a root directory references a file in a subdirectory, then how could this reference still be valid when the two files get compiled into same assembly?
Assume a web project has the directory structure shown below.
Since all of web project’s ASPX files get compiled into a single assembly WebProject1.dll, how is this assembly able to record/memorize the directory structure?
Thus, when you deploy WebProject1.dll to a web server and user makes a request for http://WebProject1/some_SubDir/default.aspx, how will WebProject1.dll be able to figure out which Page to render?
WebProject1\SubDir (where WebProject1 is a root directory)
WebProject1 -- contains several ASPX files
WebProject1\SubDir -- contains a file default1.aspx.
When we deploy the Web project, must we create the same directory structure on a web server (WebProject1\SubDir), even though we won’t put any ASPX files into those directories?
I assume that on Web server WebProject1.dll should be placed into the Bin directory?
thanx
EDIT:
Only the sourcecode is compiled into the assembly, you still need to upload the aspx files into a matching directory on the server.
My book says that when using Web project all web code is compiled into single assembly. I thought “all code” includes aspx files?!
Links are maintained between the page and it's code behind file through a class declaration which by default is in a namespace that matches the directory structure
So if I add a new aspx page via Project --> Add New Item, and store this aspx page in a subdirectory named Hey, then this page will reside in namespace WebProject1.Hey?!
But how do I do add new item into a subdirectory, since Project --> Add New Item doesn’t give me an option to browse and choose a directory I wish to save it in, but instead automatically creates aspx file in a root directory?
The relative path is kept when the compiler generate the dll.
I’m not sure I know what relative path you’re referring to?
thanx
Only the sourcecode is compiled into the assembly, you still need to upload the aspx files into a matching directory on the server. For example you project in Visual Studio may look like the following:
WebProject1 (The root project)
|
|- some_SubDir (A physical directory inside the project)
|
|-default1.aspx
|-default1.aspx.cs (assuming a C# project)
Once you have compiled the web app you'll need to upload the following to the server:
WebProject1 (The root directory for your website)
|
|-bin (The binary directory created by the build)
|
|-WebProject1.dll (The compiled source code of the web app)
|-some_SubDir
|
|-default1.aspx (The file that will map to the URL www.websitename.com/some_subdir/default1.aspx)
Compiled resources (non-sourcecode files that are compiled and stored inside the assembly) are different issue that are addressed in your other question
Edited to add direct answers to the questions:
Not all files are compiled into the assembly, only source code files are. Links are maintained between the page and it's code behind file through a class declaration which by default is in a namespace that matches the directory structure, but it doesn't have to be.
Your default1.aspx file will have in the header something like:
The inherits line tells the webserver that when a user requests this page it should be processed in conjunction with the source code that defines that class, which it will find inside the compiled assembly. The combination of the physical aspx file and the compiled class will generate standard html which is then passed back to the client.
Yes, you need to create the same directory structure, but you are required to put the aspx files in there.
Yes
(can someone please edit this if they know how to get the list items to number correctly, please?)
All those path information will be embedded as Meta Data/resource file, so, you can deploy it safely to the server. The relative path is kept when the compiler generate the dll.
I suggest you use Reflector to open the dll, and you can get a much more deeper understanding what is inside dll.
Notice how some_Subdir/default1.apsx has a 'Inherits' key/value pair in the page declaration?
What this means is that when you make a request for that resource IIS goes 'Ah ha! Asp.Net needs to handle this request! Hey asp.net please return me some html to send down'
Asp.net parses that aspx file and creates a proxy class on the fly that inherits from WebProject1.some_Subdir._Default1. This proxy class then parses out the control tree and html, and kicks off the page life cycle (this is overly simplified, and I'm sure I've missed some details).
So the WebProject1.dll is just the actual C# / VB of your web app, but in concert with the asp.net worker process and the markup you can render html back to a client.