Get all #Html.GetResource() in a .cshtml file? - c#

We have a resource file called Resouce.NL.resx. This file contains translations for a specific language. However, alot of the #Html.GetResource() arguments dont yet exists inside the resource file.
I now have to loop through every .cshtml file to manually add all those resources.
Is there any tool or extionsion to get all #Html.GetResource() inside .cshtml files that dont yet exists within a resource file?

You can get all resources as a dictionary using System.Resources.ResourceManager in C#.
Example:
var resorces =new System.Resources.ResourceManager("YourNamespace.YourResourceFileName", Assembly.GetExecutingAssembly())
.GetResourceSet(System.Threading.Thread.CurrentThread.CurrentCulture, true, true)
.OfType<DictionaryEntry>();
Razor: You can create and use custom helper.example:
Helper:
public static class MyClass{
public static string ResourceFor<T>(this HtmlHelper html, object key) {
return new System.Resources.ResourceManager(typeof(T)).GetString(key.ToString());
}
}
Use:
#Html.ResourceFor<MyProject.Resouce.NL>("Test") //where `MyProject.Resouce.NL` is your resource namespace with class name

Related

Good practice for DeploymentItem?

I'm fairly rusty when it comes to C#. I've been poking my nose around internet trying to find a solution to my question without success.
I created a test project using MSTest. Some tests use files, that I added to my project test under the folder TestData, and they are copied when executing the test by using the attribute DeploymentItem.
Example: [DeploymentItem(#"TestData\test.txt")]
This copies test.txt at the execution folder and it works. However, when I want to use this file in the test, I then have to work on "test.txt" instead of #"TestData\test.txt". Thus, if I want to factorize my code, I have to have two variables:
const string testFileName = "test.txt";
const string testFilePath = #"TestData\test.txt";
and then use them as
[DeploymentItem(testFilePath)]
public void TestFunction()
{
[...]testFileName[...]
}
Ideally, I want instead to write:
[DeploymentItem(testFilePath)]
public void TestFunction()
{
[...]testFilePath[...]
}
This way I would only need one variable.
It would work if I use the second argument of DeploymentItem as such:
const string testFilesFolder = "TestData";
const string testFilePath = #"TestData\test.txt";
[DeploymentItem(testFilePath, testFilesFolder)]
public void TestFunction()
{
[...]testFilePath[...]
}
However, that forces me and everyone to think about passing the second argument every time we use DeploymentItem. But it has the merit of working.
Here are the different things I tried to do to address the issue:
Inheriting from DeploymentItem to simply add my own constructor: DeploymentItem is sealed so this is not possible.
Creating my own attribute, by copying the code of DeploymentItem. The file is not copied at all:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
class DeployFileAttribute : Attribute
{
public DeployFileAttribute(string path)
{
Path = path;
OutputDirectory = System.IO.Path.GetDirectoryName(path);
}
public string Path { get; }
public string OutputDirectory { get; }
}
[DeployFile(testFilePath)] // testFilePath is not copied at all, even though the constructor is correctly executed.
Creating a method that would return the attribute. It does not seem like it is possible to use the result of a method as an attribute:
public static DeploymentItemAttribute DeployFile(string path)
{
return new DeploymentItemAttribute(path, System.IO.Path.GetDirectoryName(path));
} // No compilation error
[DeployFile(testFilePath)] // DeployFileAttribute type does not exist
Creating something like a C++ style using statement or C style macro, I can't seem to find a syntax that works
using DeployFile(string toto) = DeploymentItemAttribute(toto, System.IO.Path.GetDirectoryName(path)); // Syntax is wrong, could not find one that works
Any hindsight would be welcome!
From my point of view, there are only two possibilities:
You use DeploymentItem in the way it was created by Microsoft.
[DeploymentItem(testFilePath, testFilesFolder)] as you manshioned in your post
You can combine source path:
const string testFileName = "test.txt";
[DeploymentItem(#"TestData\" + testFileName)]
public void TestFunction()
{
[...]testFileName[...]
}
In this case, you'll have just one variable :)
You can write your own extension for MSTest and create an attribute you need. But this is not the easy way. As key words for this approach, you could google for TestExtensionExecution, ITestMethodInvoker and TestClassExtensionAttribute
On the other hand, this is very understandable, why DeploymentItem is implemented as it is. Do not forget, that the source folder can be an absolute path as well. So assume, that you have the following attribute [DeploymentItem(#"S:\Shared\TestFiles\AAA\BBB\test.txt")] What should be the destination folder? But even with relative paths: [DeploymentItem(#"..\..\..\TestFiles\AAA\BBB\test.txt")] - can say the name of the destination folder in this case?

BundleTransformer.Less inject variables depending on context/request

We would like the use the bundling mechanism of System.Web.Optimization in combination with the Less transformer.
The problem is that the same application/server serves pages for different branded websites. So depending on the 'SiteContext' the same .less files are used but different values should be used by the .less variables. So we want the (re)use the same less files but with different variables depending on the context of the request.
I tried a couple of different theories:
In all 3 cases I setup different bundles depending on the SiteContext.
1 inject an #import directive with the themed variables by using a custom VirtualPathProvider that intercepts the variables.less file.
So I have:
the styling file eg: header.less (imports the variables file)
the variables file: variables.less
a themed variables file: variables-theme.less (injected in variables.less via the VirtualPathProvider)
This is not working because the BundleTransformer cache sees this as the same file and doesn't know about the SiteContext. The cache key is based on the Url of the IAsset and we cannot influence this behavior.
2 Replace the variables.less import by variables-themed.less with an custom transformer that runs before the Less transformer.
Again no luck, same caching issues.
And as a side effect, the extra transformer was not called in debug because the assets are not bundled but called individually by the LessAssetHandler. This could be solved by writing your own AssetHandler that calls all required transformers.
3 create themed Asset names that are resolved by a custom VirtualPathProvider
Eg. Add header-themeX.less to the bundle, this file doesn't exist but you resolve this file to header.less and use method 2 to set the correct variables file import. (replace the import of the variables.less to the themed version).
Once again no luck. I think this could solve the caching issue if it wasn't for the Bundle.Include(string virtualPath) that does a File.Exists(path) internally. It doesn't pass via the CustomVirtualPathProvider.
Am I looking to deep to solve this?
All ideas are welcome, I can imagine that this will become a problem to more and more people as the System.Web.Optimization library gets more popular...
Keep in mind that:
we have a lot of .less/css files
we will have 5 or so themes
we like to keep things working in visual studio (that is why header.less has a ref. to variables.less)
Thanks for any feedback.
Michael!
You use the Microsoft ASP.NET Web Optimization Framework and the Bundle Transformer in multi-tenant environment, so you need to replace some components of the System.Web.Optimization and create own versions of the debugging HTTP-handlers (see «Problem: LESS file imports are added to BundleResponse.Files collection» discussion). As far as I know, Murat Cakir solve all these problems in the SmartStore.NET project.
In the Bundle Transformer there are 2 ways to inject of LESS-variables:
Look a properties GlobalVariables and ModifyVariables of LESS-translator:
using System.Collections.Generic;
using System.Web.Optimization;
using BundleTransformer.Core.Builders;
using BundleTransformer.Core.Orderers;
using BundleTransformer.Core.Transformers;
using BundleTransformer.Core.Translators;
using BundleTransformer.Less.Translators;
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
var nullBuilder = new NullBuilder();
var nullOrderer = new NullOrderer();
var lessTranslator = new LessTranslator
{
GlobalVariables = "my-variable='Hurrah!'",
ModifyVariables = "font-family-base='Comic Sans MS';body-bg=lime;font-size-h1=50px"
};
var cssTransformer = new CssTransformer(new List<ITranslator>{ lessTranslator });
var commonStylesBundle = new Bundle("~/Bundles/BootstrapStyles");
commonStylesBundle.Include(
"~/Content/less/bootstrap-3.1.1/bootstrap.less");
commonStylesBundle.Builder = nullBuilder;
commonStylesBundle.Transforms.Add(cssTransformer);
commonStylesBundle.Orderer = nullOrderer;
bundles.Add(commonStylesBundle);
}
}
Create a custom item transformation:
using System.Text;
using System.Web.Optimization;
public sealed class InjectContentItemTransform : IItemTransform
{
private readonly string _beforeContent;
private readonly string _afterContent;
public InjectContentItemTransform(string beforeContent, string afterContent)
{
_beforeContent = beforeContent ?? string.Empty;
_afterContent = afterContent ?? string.Empty;
}
public string Process(string includedVirtualPath, string input)
{
if (_beforeContent.Length == 0 && _afterContent.Length == 0)
{
return input;
}
var contentBuilder = new StringBuilder();
if (_beforeContent.Length > 0)
{
contentBuilder.AppendLine(_beforeContent);
}
contentBuilder.AppendLine(input);
if (_afterContent.Length > 0)
{
contentBuilder.AppendLine(_afterContent);
}
return contentBuilder.ToString();
}
}
And register this transformation as follows:
using System.Web.Optimization;
using BundleTransformer.Core.Orderers;
using BundleTransformer.Core.Bundles;
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
var nullOrderer = new NullOrderer();
const string beforeLessCodeToInject = #"#my-variable: 'Hurrah!';";
const string afterLessCodeToInject = #"#font-family-base: 'Comic Sans MS';
#body-bg: lime;
#font-size-h1: 50px;";
var commonStylesBundle = new CustomStyleBundle("~/Bundles/BootstrapStyles");
commonStylesBundle.Include(
"~/Content/less/bootstrap-3.1.1/bootstrap.less",
new InjectContentItemTransform(beforeLessCodeToInject, afterLessCodeToInject));
commonStylesBundle.Orderer = nullOrderer;
bundles.Add(commonStylesBundle);
}
}
Both ways have disadvantage: the injection of LESS-variables does not work in debug mode.

Using application's *.resx from a generic DLL

I'm creating a generic DLL that creates documents from models and can be used either for winForms or for webForms. It's composed of a main class that I instantiate with certain parameters.
I'd like my DLL to be able to lookup in the resource files without being tied down to 1 technology.
So to say, I know how to access my resource files (*.resx) in a WebForm :
HttpContext.GetGlobalResourceObject("Global", "myLabel")
I have a few restrictions :
I don't want to transfer the HttpContext to the DLL as it will tie it to the application
I don't want to rename the resource files from *.resx to *.resource because they are used in the application
I don't want to pass all the labels over to the DLL because then my models won't be modifiable as I need
I'd like to place a marker in my document models that is like this <%resource(Global,myLabel)%>
I've been looking at passing the class a resource object using ResourceManager but it never gets hold of my *.resx files.
Does anyone know how to acheive the final goal? Either passing a resource object to the class either picking up the resource object from inside the class.
Ok, I found how to do this.
In my DLL, I overloaded the constructor allowing to give it a ResourceManagerobject. So here is what I have :
For the model file :
\paragraph
[
Style = "Normal"
]
{
<%resource(lblMontant)%> : <%montant%>
}
For the DLL using the ResourceManager :
public Reporter(String inputModel, String outputPdf, Dictionary<String, IParameter> parameters, ResourceManager resman)
{
// Assigne parameters to globals
_sourceFile = inputModel;
_destinationFile = outputPdf;
_parameters = parameters;
_rm = resman;
Worker();
}
private String parseResource(String val)
{
MatchCollection _matches = _resourceMatcher.Matches(val);
foreach (Match _match in _matches)
{
String _item = _match.Groups["item"].Value;
val = val.Replace(_match.Groups[0].Value, String.Format("{0}", _rm.GetObject(_item)));
}
return val;
}
For the caller, we use Resources.global that is considered a class :
ResourceManager _rm = new ResourceManager(typeof(Resources.global));
Reporter _cl = new Reporter(modelFilePath, outputFilePath, _params, _rm);
If this is useful to others and some need more details, don't hesitate to ask ;)

OFX File parser in C#

I am looking for an OFX file parser library in C#. I have search the web but there seems to be none. Does anyone know of any good quality C# OFX file parser. I need to process some bank statements files which are in OFX format.
Update
I have managed to find a C# library for parsing OFX parser.
Here is the link ofx sharp. This codebase seems to be the best case to startup my solution.
I tried to use the ofx sharp library, but realised it doesn't work is the file is not valid XML ... it seems to parse but has empty values ...
I made a change in the OFXDocumentParser.cs where I first fix the file to become valid XML and then let the parser continue. Not sure if you experienced the same issue?
Inside of the method:
private string SGMLToXML(string file)
I added a few lines first to take file to newfile and then let the SqmlReader process that after the following code:
string newfile = ParseHeader(file);
newfile = SGMLToXMLFixer.Fix_SONRS(newfile);
newfile = SGMLToXMLFixer.Fix_STMTTRNRS(newfile);
newfile = SGMLToXMLFixer.Fix_CCSTMTTRNRS(newfile);
//reader.InputStream = new StringReader(ParseHeader(file));
reader.InputStream = new StringReader(newfile);
SGMLToXMLFixer is new class I added into the OFXSharp library. It basically scans all the tags that open and verifies it has a closing tag too.
namespace OFXSharp
{
public static class SGMLToXMLFixer
{
public static string Fix_SONRS(string original)
{ .... }
public static string Fix_STMTTRNRS(string original)
{ .... }
public static string Fix_CCSTMTTRNRS(string original)
{ .... }
private static string Fix_Transactions(string file, string transactionTag, int lastIdx, out int lastIdx_new)
{ .... }
private static string Fix_Transactions_Recursive(string file_modified, int lastIdx, out int lastIdx_new)
{ .... }
}
}
Try http://www.codeproject.com/KB/aspnet/Ofx_to_DataSet.aspx. The code uses Framework 3.5 and transforms an ofx into a dataset, this may help with what you're trying to do.

In ASP.NET MVC, how do you retrieve the filename of the script being run?

I need to get the name of the script being executed on the master page to update the Last Write time.
I'm using this:
System.IO.File.GetLastWriteTime(Server.MapPath(Request.FilePath))
which works for the default.aspx, but if its within a View I am unable to workout what the physical path to the file is to get the LastWriteTime.
Is there a solution to this? Surely I'm missing something incredibly easy here.
Thanks!
From what I know about System.Web.Routing, the routing happens in IIS, making it impossible to get the true physical path of your View.
You can always try to use
this.Request.PhysicalPath
to get the physical path but it will return something like :
C:\Projects\MySolution\MyProject\ViewFolder\ViewAction
Based on this answer and this one too, I wrote a html helper like so:
public static string ScriptBlock(this HtmlHelper html, string path)
{
return string.Format("<script src=\"{0}{1}\" type=\"text/javascript\"></script>\r\n", VirtualPathUtility.ToAbsolute(path), DateLastWriteFile(html.ViewContext.HttpContext.Server.MapPath(path)));
}
public static string CSSBlock(this HtmlHelper html, string path)
{
return string.Format("<link href=\"{0}{1}\" type=\"text/css\" rel=\"Stylesheet\" ></link>\r\n", VirtualPathUtility.ToAbsolute(path), DateLastWriteFile(html.ViewContext.HttpContext.Server.MapPath(path)));
}
public static string DateLastWriteFile(string path)
{
return "?Date=" + File.GetLastWriteTime(path).ToString("yyyyMMddhhmmss");
}
Then, I simply call the method in my master or view pages, after including the proper namespace at the top of the view/master page obviously:
<%= Html.CSSBlock("~/Content/Site.css") %>

Categories