Running ASP.NET on Mono Resource Access Errors - macos

Summary of Solutons: The core issue was that I had my resx files in subfolders within App_LocalResources and App_GlobalResources. Separating things into folders is fine on .NET but Mono only looks at the files in those folders ignoring any sub folders.
Issue #2 works only at runtime. In other words the Resources namespace can be used in the aspx or in code in a script block in the page but not in the code behind. It seems that I've never used the Resources namespace in the code-behind proper so all 3 of my issues are solved.
I have an ASP.NET application I was previously working on on .NET on a PC and am trying to move over to a Mac to develop using Mono and MonoDevelop. The application utilizes the App_GlobalResources and App_LocalResources folders. The application worked on .NET but does not function properly on Mono as I will outline. The version information is below:
OS: Mac OS 10.8.2
Mono: 2.10.9
ASP.NET: 4.0.30319.1
MonoDevelop: 3.0.5
I've tagged this with MonoDevelop because I'm not sure what server is being used and whether it is a part of MonoDevelop or Mono itself or is separate. I'm not familiar enough yet with Mono and it's parts...
The issues I'm having revolve around accessing resources and I'm having various related issues which I'll outline below:
Issue 1: meta:resourcekey doesn't work and "a resource object was not found at the specified virtualPath." error
I have App_LocalResources folders and resx files for each page at the root of the application as well as in sub-folders where page localization is required. Elements on the page that have a meta:resourcekey attribute do not have the relevant property assigned the value in the resx file.
So for example I have: /Site/Home.aspx with a /Site/App_LocalResources/Home folder which contains home.aspx.resx and related translations
N.B. as I was writing this I realised the names don't match up with regards the files case, but that is not the issue, I've tested with another page in the same folder and it doesn't render out the resource values.
The page in question has no code behind.
A different page in the root of the application called PageNotFound.aspx has related resx files in /App_LocalResources/PageNotFound/PageNotFound.aspx.resx
This page content is basically as follows (if I remove both the page renders)
<h2><asp:Literal runat="server" meta:resourcekey="PageNotFound" /></h2>
<% string message = GetLocalResourceObject("Sorry.Template").ToString(); %>
I get the same error whether I remove the first or the second of these lines. The error starts:
System.Resources.MissingManifestResourceException: A resource object was not
found at the specified virtualPath at
System.Web.Compilation.DefaultResourceProvider.GetLocalResourcesAssembly
If I remove the first line is actually builds and tries to render the page but crashes giving the above error.
If I remove the second line I get the error as well but it is while the page is being parsed and the runtime is trying to build the page class.
So for one page is seems build and run with meta:resourcekey attributes but isn't binding the resource value to the properties of the underlying controls, on another page it seems to crash trying to access the resources. The odd thing is the page that crashes is a very simple page without a master page and it basically only has the above content, the other page is quite complex with a master page and it renders (i.e. no parser error) but without the resourcekey values assigned....
The only differences that I can see are that one page is in the root directory and no master page, the other is in a sub folder and has a master page? Go figure! Any ideas?
Issue 2: Resources namespace doesn't exist
If I access a page that uses the 'Resources' namespaces that is available in .NET, I get a runtime compilation error, as in the app builds when compiled, but as the page is parsed and compiled it causes a (runtime) exception...
Compilation Error: CS0103: The name 'Resources' does not exist in the current context
The code would be in the code behind: (EDIT: The following line I think, was me testing, my code never uses this namespace in a code behind (compile time) class. Once the global resources functioned then the namespaces worked at runtime, but not in scenarios where the namespace needs to be verified at compile time as outlined in the summary at the top of the question)
msg.Subject = Resources.EmailResources.Request_SubjectLine
or in a script block in the aspx itself i.e.
<%= Resources.EmailResources.Request_SubjectLine %>
Neither of these types of access to the Resources namespace work. Why does this not work in Mono. Does the Mono framework not generate this stuff for us like .NET does? (I'm fairly sure this namespace hierarchy is generated by the ASP.NET framework on .NET and therefore wonder if this just has not been implemented in Mono?)
Issue 3: GetGlobalResourceObject(...) Call Returns Null
In the code-behind I have calls to GetGlobalResourceObject() which works on .NET, but when I run it on Mono my code is throwing an exception. For example:
hlContact.Text = GetGlobalResourceObject("CommonResources", "Contact").ToString();
My folder structures is:
App_GlobalResources > CommonResources > CommonResources.resx and the designer file (CommonResources.Designer.cs) is there.
The resx files are all marked with build action of "EmbeddedResource", "Do not copy" use the Custom Tool "GlobalResourceProxyGenerator" and have a resource ID that makes sense for each file. (i.e. .App_GlobalResources.CommonResources)
The issue is that GetGlobalResourceObject() returns null... It's not finding the CommonResources resource....
Note that Mono is constructing '*.resource' files in the folder with the resx files.

Issue #1
Apparently mono doesn't search subdirectories for resources, that is you must keep them directly in App_LocalResources or App_GlobalResources. See the relevant part in the mono source code (line 134). If searching subdirectories is an official feature, then this is a bug, and can be easily fixed by changing the aforementioned line.
Issue #2
Works fine here (on linux).
Issue #3
I don't know how exactly you are embedding the resources, a small test project would help. For the App_GlobalResources the framework compiles them at runtime and then GetGlobalResourceObject works fine too.

Related

Is there software I can use to view my Markdown files as a wiki, with relative links, on Windows

Our team has installed the Markdown Mode extension in Visual Studio on our Windows PCs, and we're happy with that as an editor for Markdown files, but we need a way to generate a wiki from those files where we can click on links that cross-link the files of the wiki. I've been trying to find something, but haven't had any success getting something running.
I tried creating an empty web application and pasting in the html file from here http://dynalon.github.io/mdwiki/#!index.md and naming it index.html, and adding a couple of md files to the same directory that I set to always copy to the build directory, but I got 404-3 errors when it tried to access the .md file.
I see a couple of tools that look possibly good but need Python or Ruby installed, which isn't ideal: http://markdoc.org/quickstart or http://helloform.com/projects/commonplace/
I see this ASP.NET control for embedding a Markdown file into a page http://wikicontrol.codeplex.com/ but the control is for VS 2010 so clearly is not being actively maintained, plus to use it I'll need to build something to take the relative links and find the related .md files and load them up in MVC - sounds like a hassle to get working, and it will require me to put MVC in my docs project.
Is there something that is just designed so that I can put an html file or similar in a directory with a root .md file and have it just immediately act like a wiki and allow navigation between them?
We have decided to use MarkdownDeep NuGet package and a single MVC controller to handle this. The MVC controller looks at the requested path, uses it to figure out the location of the Markdown file, reads that file and renders it to HTML and returns the HTML.

Fail to load assembly on a MVC View

Some Context
I have one closed source asp.net MVC application distributed for several customers. I'm trying to develop a extension to include some very specific functionalities that will be used by a single customer (he will also have the extension source code).
I'm developing the extension as another mvc project, loaded as an area. I'm trying to avoid having to deploy the extension binaries in the main application '/bin' folder.
I'm loading the extension assembly and it's dependencies manually in the PreApplicationStartMethod of the main application assembly. The area registration process went fine and the area routes are registered as intended.
The Problem
When I try to load some extension pages, I got a "The view 'xx' or its master was not found or no view engine supports the searched locations." message. I investigated I little and replaced the view contents with a 'Hello' string. The view was rendered correctly.
I tried to produce a minimal that triggers the error and turns out it's the '#model'directive. I tried to figure out what's happening and It's an assembly loading error. I created a minimal view with '#model object' and tried to cast the Model to original type and got the following error message: "Could not load file or assembly 'xxx' or one of its dependencies. The system cannot find the file specified." (this assembly contains the class type used by the view)
Just to confirm the issue, I moved the mentioned assembly to the main application '/bin' folder and everything worked.
I also inserted some model manipulation instructions on the controller, just to investigate if an exception will appear but everything went fine, I could even render a grid using json.
I'm loading every assemly with:
var assembly = Assembly.LoadFrom(file)
BuildManager.AddReferencedAssembly(assembly)
There's something extra I need to do ?
Needed to add the assembly private path
AppDomain.CurrentDomain.AppendPrivatePath(pluginFolder);
Don't know the real explanation for it but I guess it's relates to the use of Assembly.LoadFrom(file) instead of Assembly.Load() and the fact that views are compiled later.

References for MVC extension methods differ between vbhtml and cshtml files?

I am converting the login views of an application from vbhtml to cshtml. In the vbhtml files the original developer calls an Html.LabelWithAttributesFor extension method. When I write the same line of code in the new cshtml file the extension method isn't available via Intellisense and fails to compile. Are references not shared between vbhtml and cshtml files in a project?
Wanted to circle back and close this question with the "answer". In the end, what seems to have knocked something loose was when we removed the vbhtml pages from the solution. All use of the Extensions had been removed prior to our removing the vbhtml pages and other developers had worked on the solution in the interim making other changes. Fast forward a few months to another developer needing the extensions. He added a reference to the project with the extension methods and used them on a new cshtml page, which worked fine. So, the problem is solved but I still have no idea what caused the problem in the first place and I have been unable to reproduce it.

Global.asax.cs is not visible at the server

Running under Server08 | IIS7. I have a website project and am in the habit of hand editing the Global.asax.cs at the deployed site many times in the past. Recently I've found that only the Global.asax is present and has only:
<%# Application Codebehind="Global.asax.cs" Inherits="myDomain.MvcApplication" Language="C#" %>
There are simply no Global.asax.cs files present/visible for any of my active, functioning websites anymore.
I've checked here Global.asax can't find code-behind class and here where is the codes in Global.asax in mvc3? without getting anywhere. The second link contains the comment: 'That's because it's a compiled web application. You'll have to view it in Visual Studio as a project.'
But since my project has always been a website - and I've not converted it (intentionally) I'm puzzled by the changed behavior.
But when i step into VS12 and look at the context menu for the solution's WebProject it presents 'Convert to Web Application'. The sites' folder structures do not contain App_Data or App_Start. To my mind, these 2 fact establish at VS is treating the project as a website, why then, is the Global.cs compiled down to the /bin?
I'll close by repeating - I've hand edited these things in the past - I'm not positive but it's probably accurate to say that this is the first time I've tried to do so after installing VS12. And, coming to think on it harder - it's only recently that I've implemented the 'One Click Publishing' service so that could be coming into play.
verify? I'd like I need to edit a simple update without full re-deployment.
thx
I think the 'One Click Publishing' is most likely the culprit here. I created a sample Web Site project to experiment, and when I use the publish feature in Visual Studio, the files generated are indeed missing the Global.asax.cs file. Instead, a bin folder is created with a compilation called "App_global.asax.dll" even though it is a Web Site and not a Web Application. I am guessing this might be similar to what is happening for you as well.
If not, I found a link which might be useful to you in order to once again be able to edit a class on the server. Particularly, have a look at the second answer (the one not accepted as the best answer) to recreate a class file for the global code: Where is the Global.asax.cs file?
I tried this solution, and verified that the class file is editable on the server, and that it is dynamically compiled at run time (the modifications I made to the file worked immediately).
Hope this helps!
After editing the global.asax you need to rebuild and upload the DLL to the bin folder. Otherwise, your changes will not take effect.
You might be able to upload an web version of the global.asax that includes the code in that one file, which obviously does not require a .cs code behind. I used to do it with aspx files in an application but I have never tried it in the GLobal.asax file.

Best way to deploy and reference an XSLT file

In a visual studio project I have three layers, Data Layer, Business Layer and Presentation Layer.
In the Data Layer I have a few XSLT's that transform some objects into an email, all works fine but I have discovered that the XSLTs do not get built/copied when building.
I have currently, created a folder in the deploy location and placed the XSLT's there but I am concerned about relying on a manual process to update these.
Has anyone encountered a similar issue and if so how did they get around it.
It smacks of changing the MSBuild script to copy the build artifacts to the required location, does anyone have examples of this?
Thaks
If you are using Visual Studio 2005/2008, the easiest way to do this is by including your XSLT files as project resources.
Open the Properties for your project.
Select the Resources tab. You will probably see a link that says "This project does not contain a default resources file. Click here to create one." Go ahead and click on that.
Click the Add Resource drop-down near the top and select Add Existing File.
Browse to your XSLT files and select them.
After you have done this, you can easily access the resources in the following manner:
// To get the contents of the resource as a string:
string xslt = global::MyNamespace.Properties.Resources.MyXsltFile;
// To get a Stream containing the resource:
Stream xsltStream = global::MyNamespace.Properties.Resources.ResourceManager.GetStream("MyXsltFile");
If you are using Visual Studio 2003, your best bet is to include those XSLT files as embedded resources for the DLL. In Visual Studio, select the file(s) in Solution Explorer, open the Properties pane, and change the Build Type to "Embedded Resource". You can then use the GetManifestResourceStream method to get a Stream containing the XSLT(s). The name to pass will be based on the default namespace of your assembly, the folder containing the file, and the name of the file.
For example, say your data layer assembly has a default namespace of My.DataLayer. Within your data layer project you have a folder named Templates which contains a file called Transform.xslt. The code to get your XSLT would look like this:
// There are numerous ways to get a reference to the Assembly ... this way works
// when called from a class that is in your data layer. Have a look also at the
// static methods available on the Assembly class.
System.Reflection.Assembly assembly = (GetType()).Assembly;
System.IO.Stream xsltStream = assembly.GetManifestResourceStream("My.DataLayer.Templates.Transform.xslt");
For more information check out this article on CodeProject.
Obvious question maybe, but still has to be asked, did you include the folder containing the XSLT's in the project itself? Is this a web or forms app?
In VS, it is easy to set the properties of the XSLT files in the project to copy on build, by default they do not.
I may have explained myself poorly.
THe Data layer is a class library that a the presentation layer references.
On building the DataLayer I can get the XSLTs to output to the Bin directory of the DataLayer. However when I build and publish the presentation layer, it correctly grabs the DLL but not the XSLTs

Resources