Best way to deploy and reference an XSLT file - visual-studio

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

Related

Adding a custom property to the VS Project Designer

I am creating a Visual Studio extension and have added a custom configuration-specific property to an existing Microsoft Visual C++ (vcxproj) project using the IVsBuildPropertyStorage interface.
I would like to have that custom property show up in the Project Designer's property pages, as part of a new property page. There is some information on how to achieve that for a custom project type but the approach relies on reimplementing the project system which is not appropriate for all situations. Is there a way to extend the Project Designer for an existing project type?
One way to customise the Project Designer pages for a vcxproj is to include an additional property schema by adding the following:
<ImportGroup Label="PropertySheets" />
<ItemGroup>
<PropertyPageSchema Include="proppage.xml">
<Context>Project</Context>
</PropertyPageSchema>
</ItemGroup>
to a project which can be done by either copying this snippet into the project file directly or by putting it into a custom props file and including that into the project. Details on the structure of a property page schema are available here.
The main issue with this approach is that it requires manual intervention or an explicit step for setting up a project to use the custom property page, making it unsuitable for my use case.
It is also bit cumbersome as it requires shipping one-two additional files with the extension and it leaves a footprint in the project file. I would like to come across a more minimalistic solution that automatically applies to all MS VC++ projects but does not modify the project files unless the user inputs a property into the custom property page.

Is it possible to configure a csproj file to allow any project item type?

I'm working on a unique project in that I would like to be able to have CSHTML Razor Views, Windows Forms, and various other "non project type crossing" project item templates in one single project in visual studio.
Is there a way I can override the project type so that it compiles as a Windows Form's project but still allows me to add CSHTML files and other template types to it?
When you edit the .csproj using notepad or an xml editor you'll find that <ProjectTypeGuids> element.
You can then edit it to have an entry like below
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Note: these are Guids from a portable class library project. You need to find correct ProjectTypeGuids for your requirements and add it.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/d9d05cdc-96a1-4044-95d8-a4f8885a660a/what-is-the-significance-of-projecttypeguids-tag-in-the-visual-studio-project-file?forum=vsx

Running ASP.NET on Mono Resource Access Errors

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.

ASP.NET MVC3 project does not always publish all views/content

This is crazy, but I can't seem to get all my views/content/scripts published when I publish the site. This seems to happen, I believe, when the view or content is not directly referenced by my project, but used by another assembly in my project. So I might have:
ExternalAssembly.dll referenced (it gets published)
I'll need ExternalLogin.cshtml in my main project, under my views folder
ExternalLogin.cshtml doesn't get published
Right now I have a script that copies everything in the Views folder and dumps it to where I want it deployed, but VS should do this for me. What am I doing wrong?
When you click on one if these files what is the build action for it on the properties? Content....or? Set to content.
So your views files are in another project or folder outside your current project? Normally the files have to exist in the web site project, in it's views folder, not externally, and the build action should be set to Content and not to copy to the output folder. But there are some workarounds:
Duplicate them in to your site views folder and make sure they are marked content (as stated in another answer). One thing to note though is that you can add them as "Linked Files" in visual studio which actually allows them to exist in two places in the hierarchy without having to exist in two places on disk: http://support.microsoft.com/kb/306234
If you have control over the external library, you can compile them in as embedded resources or use Razor Generator or something similar and use a custom view engine to return them: How can I make ASP.NET MVC 3 use views (aspx, ascx) files from an external assembly in my website?
Manually put the copies in the .csproj build XML using the Copy task: http://msdn.microsoft.com/en-us/library/3e54c37h.aspx (Note that this will make it work in visual studio doing essentially what you are doing now, as it will then be part of the Visual Studio build if you add it to the AfterBuild target or something)

Visual Studio: How to store an image resource as an Embedded Resource?

By default when you add an image (icon, bitmap, etc.) as a resource to your project, the image's Build Action is set to None. This is done because the image is magically stored inside a .resources file.
I want the resource to be stored as an embedded resource (my reasons are irrelevant, but let's just pretend it's so that I can see them inside RedGate's Reflector).
So I changed each image's Build Action to Embedded Resource, and the resource then appears inside Lutz's Reflector - exactly as I want.
Unfortunately, Microsoft says specifically not to do this:
Note that when the resource editor
adds an image, it sets Build Action to
None, because the .resx file
references the image file. At build
time, the image is pulled into the
.resources file created out of the
.resx file. The image can then easily
be accessed via the strongly-typed
class auto-generated for the .resx
file.
Therefore, you should not change
this setting to Embedded Resource,
because doing so would include the
image twice in the assembly.
So what is the proper way to include an image as an embedded resource?
Note: This answer is not the recommended way of handling image resources. It just addresses the particular problem as described by the question (i.e. to include an image as an embedded resourse).
Don't add the image as a resource. I would rather do the following:
Create the image/icon and save it to a file
Choose Project -> Add Existing Item and add the file
Set the Build Action to Embedded Resource
You can then access this resource using
Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceUri)
This way the image is not magically added to the projects resource file and you will only have one copy of the image stored in the assembly's resources.
In the project view, open the Properties subtree; double-click the Resources.resx item.
(If you don't have one, right-click the Properties item and choose Open, then click on the Resources tab on the left, and click the link in the middle of the page to create a default resources file.)
click the first droplist at the top of the resources.resx page. It probably says something unhelpful like "Strings", and select Images. Alternately you can press Ctrl + 2. This changes to the Image resources view. Now click the Add Resource droplist and choose 'existing file'. Select your image file and click Open. Rename the resource if you like.
Click the Save icon.
You can now access your embedded resource programmatically as:
[namespace].Properties.Resources.[yourResourceName]
In VS 2005:
Right click on your project and
choose add new item.
Add a resource file. Call is
myImages.resx
Place this file in the root folder of the project.
Right click on myImages.resx
and choose View Designer
Select Add Resource, Add Existing
file.
Browse for the image e.g. stop.bmp
This image does not have to be included in the project at this stage. The resource file will automatically do this.
To reference the image in code use something like:
btnStop.Image = myImages.Stop;
This depends on how you want to use the image.
If you want to localize and access specific images for a specific culture, then using the ResourceManager is a good way to go because it provides some nice functionality for satellite assemblies, searching, fallbacks, etc.
If you just want to embed a resource in an assembly and aren't worried about localization (using Assembly.GetManifestResourceStream) then just adding a image and setting the build action to Embedded Resource is fine.
The documentation warns you about not setting the build action to Embedded Resource because the resource is already compiled into the assembly by the resource compiler via the .resx file.

Resources