Where to keep resource file in asp.net mvc application? - c#

I am working on a MVC Application, which has WCF as back end.
I have all POCOs in my Data Entities CS project, which is shared between WCF and Web application projects. Now, I have a scenario, where I need to convert an MVC application to multilingual.
I have created one resource project, and given reference of it to both Entities project (to use display attributes) and Web.
But it copies the Resource dll to WCF as well, which I feel is wrong.
So, where can I put my resource files?
If in Web, then how to use Data Annotation attributes?
I am using Display attributes on Enums, instead of description to display it in multilingual form. So again, where should these Enums be (I kept them in Entities)?

Instead of adding a reference to your project. Try adding just a reference to the DLL within the bin folder of the project your trying to share. That way when you reference from 2 projects, each project points to just that one dll. In production you should setup a folder parent of your root web directory that contains those "shared" DLLs.

Related

JSON Config file shared between projects in same solution

Im writing an web app that consists of a 3 projects. The ASP.net MVC5 website, a windows service which runs jobs which users schedule using the website, and then a class library which the other two projects reference. I have a JSON config file that the class library references. It contains data as to how jobs should run. I want to include it in my source control and I want it to be JSON so my colleagues who use the app can easily change it without having to recompile the solution. Only the class library needs to access the json file - the other two projects reference the class library so they essentially reference the settings in the json file by accessing classes in the class library.
My issue is, i am referencing the json config file path in the class library classes like so: AppDomain.CurrentDomain.BaseDirectory + "config.json"
However, when the web server instantiates a class from the class library the path resolves the the base directory of the web server instead of the class library, which isn't surprising.
Whats the best way to do this? Can I keep the config file permanently in the webapp project and then have a link to the config file in the windows service project perhaps? Not sure how that would work after deployment of the solution though?
Web project is .NET standard MVC 5. JSON much preferred over XML for config file type.
I've just tested it to make sure, so this should work:
If your file is in your class library project, you can just go to solution explorer, right click on it, > properties, then in the window look for copy to output directory. Set that to Always or only if newer which ever you feel is best, and it should be copied to your bin folders of your projects that reference the class library.

How to convert .dll.refresh to a project reference when converting an ASP.NET website to a Web Application?

I have a slew of ASP.NET websites that need to be converted to Web Applications. These sites all reference the same massive set of *.dll.refresh files to get assemblies from a shared lib folder that has a deep directory structure (we're using source control with the .dll.refresh files). Manually referencing these 120+ assemblies in the new web application projects is very cumbersome (having to include them one-at-a-time since you can't just recursively grab them through a folder structure), and the process will need to be repeated several times for each conversion. Is there a way to easily convert these .dll.refresh files into assembly references, or perhaps, is there a better way of getting the assemblies referenced?

Accessing HostingEnvironment.MapPath() within class called by webservice / website

I have a project which contains shared functionality that can be used both by a ASP.NET WebApplication and a WCF Service.
The shared project is a Class Library, and has some dependencies on files.
Normally I would simply call HostingEnvironment.MapPath() in order to resolve these dependencies, however this isn't possible in this context since System.Web isn't available (it's a class library).
Is my only real option passing the absolute path to the Class Library? This would require me to do some further processing as it would be the root for the respective projects, and not the root of the Class Library.
In the end I've found the following solution:
Set the properties on the dependent files: Copy to Output Directory = Copy if newer
This copies the files into the bin folder of the Hosting Environment (so it will copy it into either the WCF Service or the WebApplication from the Class Library)
HostingEnvironment.ApplicationPhysicalPath then returns this location
The difference is the files are being copied cross project when building (I didn't know copy if newer did this). I can therefore access the files from the applications' bin (and not from the Class Library as I was attempting before).

Sharing assets between projects

I'm currently in the process of reorganizing a rather large .net mvc project by splitting it into smaller projects, and I can't seem to find a simple way to directly reference some of the assets (js, css, view templates, etc) in the original project. Optimally, what I'd like to do is have a separate project (let's call it Presentation) to store any new assets for the application, and reference them as if it were another path e.g. ~/Presentation/js/example.js. I originally looked at creating a VirtualPathProvider to solve the problem, but I can't seem to get it to work the way that I'd like. Any suggestions?
Update
I think I'm overcomplicating the issue. The assets aren't the issue (I created an IIS virtual directory for those), it's the Views that are the problem. I'm trying to split the views into two different projects, but the view engine won't find the views in the new project. I'm looking at using precompiled views to solve that problem (e.g. RazorGenerator), but I'm not sure if it's the right solution yet.
Right click on the project folder and select "Add.." "Existing item". You can store all the scripts or files in a single, shared directory, but since they are source or content files they need to be added to the project that is using them, otherwise they won't be deployed.

App_Code and server

I'm having a "tiny" issue with my App_Code folders.
I'm learning ASP.NET and, therefore, ordered a webserver with the support of ASP.NET 4.0. I'm using Visual Web Developer to program my webpages. When I upload my website to this webserver everything runs fine.
However, if I then add another web project to my server, my App_Code folder gets all messy. The server wants all my class files in the App_Code folder in the root. Is there any way I can create subdirectories in my App_Code folder or something to keep my projects organized or am I missing the point here?
You should take a look at codeSubDirectories in the web.config
Alright I found a solution to my problem. Although most of your answers might work aswell, this proved to be the best in my case. I created a subdomain and threw all files into that folder and it worked fine.
You should try to avoid using the App_Code folder for your own stuff, especially if you're using a web application project.
Whenever you convert a website to a web application project, the process actually renames your existing App_Code directory to Old_App_Code.
See Here, even though this is specific to converting .net 2.0 apps, I believe it still holds true in 4.0 since converting a 4.0 app does the same thing.:
VERY, VERY IMPORTANT: Because ASP.NET 2.0 tries to dynamically compile any classes it finds under the /App_Code directory of an application at runtime, you explictly DO NOT want to store classes that you compile as part of your VS 2005 Web Application Project under an "app_code" folder. If you do this, then the class will get compiled twice -- once as part of the VS 2005 Web Application Project assembly, and then again at runtime by ASP.NET. The result will most likely be a "could not load type" runtime exception -- caused because you have duplicate type names in your application. Instead, you should store your class files in any other directory of your project other than one named "app_code". This will be handled automatically by the "Convert to Web Applicaiton" command. This command will rename the folder Old_App_Code.
If you have access to a hosting control panel it's probably best to configure your hosting environment with a virtual folder for your second website and run it from the sub folder, e.g. www.example.com/project-b. The first site can still be running in the root folder, e.g. www.example.com.
So both sites will essentially be isolated from each other (just like they are now isolated as two separate projects in Visual Web Developer Express). And both sites have their own App_Code folder (and web.config file).
If you don't have access to a configuration panel, most hosting providers are willing to add a virtual folder for you, since it's really not a special requirement.
The virtual folder should show up as a regular folder in your FTP folder, usually inside the www or wwwroot folder. Now you can copy your project files into that folder.
Take care to use root-relative paths for URLs in your second project, so all links will work even when the website is run from the subfolder. Root-relative URLs look like this:
<asp:HyperLink runat="server" NavigateUrl="~/Default.aspx" />
<asp:Image runat="server" NavigateUrl="~/images/logo.png" />
This will automatically go to www.example.com/project-b/Default.aspx and www.example.com/project-b/images/logo.png when the website is deployed in the virtual folder.
If you need to re-use code from one site in the other, it's typically best to move such code into a separate Class Library project type, and then add a reference to that project to each website project (right-click the website project, choose Add reference..., then select the Projects tab and select the Class Library project).

Categories

Resources