ASP.NET MVC Global Resource File deployment - c#

In my mvc3 web project I have an App_GlobalResources folder containing the resource file Permissions.resx.
I am able to access the values in this file when I run the site locally in visual studio using:
string value = (string)HttpContext.GetGlobalResourceObject(resourceClass, key);
However, once I deploy it to the production web server it is not able to access the values in this file. I checked and it is creating App_GlobalResources in the bin folder of the publish directory containing the file.
My first time using global resource files, is there any additional steps required to get this working once deployed to the webserver?
Thanks.

I'm pretty sure App_GlobalResources should not be created in the bin folder. Have you got Copy to output folder set? (You shouldn't need it).
In WebForms, in properties of the .resx files, we have Build Action set to Content and Custom Tool set to GlobalResourceProxyGenerator. This creates dlls for the resources in the bin folder.
However, I've not used App_GlobalResources in MVC (it makes things tricky to test outside of a web context), but there's some info here that might be useful:
http://odetocode.com/Blogs/scott/archive/2009/07/16/resource-files-and-asp-net-mvc-projects.aspx

Related

How to access files copied to output directory with CopyToOutputDirectory in ASP NET Core 6

We have an ASP NET Core 6 API application hosted on Azure App Service.
Together with the application we would like to deploy some external xml files that are used later on by the backend runtime. We have added them to the project and set CopyToOutputDirectory = true.
Have to questions:
How to access these files from code in safe way?
What we tried is
Assembly.GetExecutingAssembly().GetName().CodeBase)?.Substring(6) to get base path but it looks hacky.
_webHostingEnvironment.ContentRootPath but it returns wrong location in development (project root folder instead of /bin)
using relative path Path.GetFullName("file.xml") but it returns project root folder in development as well
We saw that there is another property that can be set on file called CopyToPublishDirectory. When it should be set? We set only CopyToOutputDirectory and it seems to work also when doing dotnet publish. Any reference?
Let's approach this problem in a different way. When we publish the project, we include the bin folder in the publish file. Then you can access .xml inside the bin folder.
Show all files and include to your project.
Open .csproj file and modify it.
And your csproj file should be like:
https://dotnetfiddle.net/qG3W8h
comment out this paragraph and add CopyToOutputDirectory, like below:
https://dotnetfiddle.net/jrccnJ
Then you can deploy it, then you can find the bin folder.
Then you can access the file gracefully.

How to remove the *.compiled Files from bin folder?

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.

Upload without cs files

I created a ASP.NET MVC project on one machine, when it came time to upload to the server, I'd just copy the Views and the Bin folder, no Controllers or Models.
However when I copied the project to another machine, the Bin folder is not being updated with a new compiled dll so I can't just copy the bin folder.
What setting do I need to tell VS2015 Community to compile the Controllers so I only need to compile the Controllers and copy just the bin folder?
Admittedly, I should've mentioned it was an ASP.NET rather than a desktop application.
It looks like what I had to do is make the site into a Web Application and there's no easy way, if there were, it'll be found at Microsoft site which the closest I found to the answer is :-
https://msdn.microsoft.com/en-us/library/aa983476.aspx
I created a new project and then copied the files across to the new one. Unless there's an easier way, thats the only solution.

How to get absolute path of another project in a solution C#

I have a solution containing two projects. One project is just for doing all data stuff and the other one, the startup project, do all the web stuff.
Now I want to get the TasksDataBase.xml from the TaskManagerHelpers class by first getting the projects root directory. But all I get is the TaskManager.Web root directory. (I call the method inside TaskManagerHelpers.cs from a controller inside TaskManager.Web)
How do I get the TaskManager.Data root directory when I'm in a class in the same project?
I've tried with theese methodes and similar ones.
HttpContext.Current.Request.PhysicalApplicationPath;
System.IO.Path.GetFullPath();
AppDomain.CurrentDomain.BaseDirectory;
Thanks in advance!
One possibility is to embed the XML file into the assembly of the class library and then read it as resource in your web application. Remember that when you publish your web application to a web server all that will get into the package will be the files of this web application. There's no physical relation to some projects that might have lived into the Visual Studio solution that this web application was part of.
You may take a look at the GetManifestResourceStream method which will allow you to read the embedded XML from the referenced assembly.
Here's an example:
// you could use any type from the assembly here
var assembly = typeof(TaskManagerHelper).Assembly;
using (var stream = assembly.GetManifestResourceStream("TaskManager.Data.DataBase.TasksDataBase.xml"))
using (var xmlReader = XmlReader.Create(stream))
{
// ... do something with the XML here
}
Bear in mind though that since the file is embedded into the assembly you will not be able to modify it. It is readonly. If you need to modify it then an alternative approach would consist into copying this file to your web application. For example a good place is the App_Data special folder. You could even setup a post compilation step that will copy the XML file in this location.
And then you can reference it easily:
string xmlFile = HostingEnvironment.MapPath("~/App_Data/TasksDataBase.xml");
using (var xmlReader = XmlReader.Create(xmlFile))
{
// ... do something with the XML here
}
In this case since the XML file is now physically part of the web application and lives on the hard drive you could also modify it.
Just because the two projects are located in the same folder tree during development, says nothing about where they'll be located at run time. It's entirely possible that that could be on different machines.
"No," you say. They'll will definitely be on the same machine in the same c:\inetpub tree. That may be true, but that's your policy, not a requirement.
If you are going to establish a hard policy about where they are located, then you can hard-code that into you code.
Right-click the XML file and select properties, then change the Copy to Output Director to one of the other settings than "Do Not Copy". That will place the file into your \bin\ folder alongside the other project output. You can then use AppDomain.CurrentDomain.BaseDirectory as your base path
IF you are running a web project, all the referenced dll files are copied to the bin directory (unless they are in the GAC) and used from there, no matter if you add a reference to another project, Visual Studio first compile it and then copies it to the bin folder of the web project. You can mark your xml file as Content (Compilation Action) and with the copy always option so it always copy it to the bin directory .... the problem is that it sometime look for this files outside of the bin folder but I think that you can handle this.

Build (and Debug) ASP.NET Web Application in a non-default folder location

I have a Visual Studio 2008 solution with an ASP.NET Web Application project. I want to change the default output folder, of said project, to $(SolutionDir)\WebApps\$(ProjectName)\bin. This I can do and when I build I get the expected assembly files in this folder. It doesn't copy the content files (aspx, etc.) to the parent folder but I have managed to fix this by hacking the project.csproj file with a custom build target.
The problem is I want to be able to debug this application using the ASP.NET Development Server, by pressing F5 in VS2008. Unfortunately the ASP.NET Dev server starts, has a "Physical Path", in the project directory rather than the parent of the output directory.
Is there any way to build my web application to a different output folder but still run the asp.net dev server to debug my site?
Thanks.
Short answer is yes, but it isn't pretty. The process I used is given below.
Unloaded the project in VS.
Manually edited the .csproj file to include a post build action that basically copies the content files (aspx, etc.) to the parent of the output folder.
For the debug options I set the project to launch an external executable. The Asp.Net Development server. Also manually set the url to launch.
What I learnt? I wouldn't do this, I'd just stick with the default and create an install/web deployment project instead.

Categories