Issue with GetLocalResource object - c#

I am trying to access my local resources file in my code-behind. I did some googling since I was unsure of how to do it and found this:
oContent.Text = HttpContext.GetLocalResourceObject("NonSupport").ToString();
However, I get an error saying that it needs at least two parameters: VirtualPath and ResourceKey. There is a third, CultureInfo but that one is optional. When I put this in as my virtual path:
HttpContext.GetLocalResourceObject("App_LocalResources/ExpandableListView.aspx.resx", "NonSupport").ToString();
I get the following compiler error message:
The relative virtual path 'App_LocalResources/ExpandableListView.aspx.resx' is not allowed here.
I must be doing something wrong with this since my searches (and some posts I found on here) say all I need to do is call the resource key.
Any thoughts? Thanks!

Did you put a resource file with the name (your aspx web page).aspx.resx into a App_LocalResource folder underneath the path where your ASPX page lives??
Furthermore, just simply call the GetLocalResourceObject method on your current page:
oContent.Text = GetLocalResourceObject("NonSupport").ToString();
No need to use HttpContext for that - the method is defined on the Page class.
Marc

Related

Call html partial from different folder C#

I have the following DIR structure:
Public
--mobile
----Views
------UK
--------Recipes
----------ViewRecipePlain.cshtml // pull this
--mobilePublic
----Views
------UK
--------Recipes
----------ViewRecipe.cshtml // into this
I want to call ViewRecipePlain.cshtml from within ViewRecipe.cshtml
Inside ViewRecipe.cshtml I'm trying:
#Html.Partial("../../../../mobile/Views/UK/Recipes/ViewRecipePlain")
But am getting the error:
Cannot use a leading .. to exit above the top directory.
Is there another way to do this?
UPDATE
I also tried:
#Html.Partial("~/mobile/Views/UK/Recipes/ViewRecipePlain.cshtml")
But get the following error:
The partial view '~/mobile/Views/UK/Recipes/ViewRecipePlain.cshtml' was not found or no view engine supports the searched locations.
The following locations were searched:
~/mobile/Views/UK/Recipes/ViewRecipePlain.cshtml
Have also update DIR structure to be more accurate if it makes a difference but doubt it does.
You can just use the absolute path of the view:
#Html.Partial("~/mobile/Views/UK/Recipes/ViewRecipePlain.cshtml")

Missing manifest error when trying to retrieve resource via resource manager

I'm having a few troubles with getting resources dynamically as I get "Missing manifest" errors. I looked up a few possible causes and did what was written there but so far nothing worked.
Currently I have this situation:
The resx file I'm trying to access is: "Resources/Messages.resx" (thus in a nonstandard folder.
The code I'm using is this:
ResourceManager resourceManager = new ResourceManager("Resources.Messages", this.GetType().Assembly);
resourceManager.GetString("ResourceText" + MessageType + "Subject")
with messageType being a string. On the second line I get the error message.
The status of the resource file is this:
Build Aciton: Embedded Resource
Custom Tool: PublicResXFileCodeGenerator
Access Modifier Public
The resfile has 2 variants: Messages.res and Messages.de.resx with the same names for each row and also the same general properties (the "GetString" also definitively tries to access the correct name).
So my question is what I'm doing wrong there and what can I do to correct this problem?
Found the problem. For new ResourceManager not only the namespace of the resourcesfiles has to be given but also the default namespace. Thus if the application has a default namespace of: MyApplication.MyServerApp then instead of "Resources.Messages" one must put in: "MyApplication.MyServerApp.Resources.Messages" leading to the following functioning sourcecode:
ResourceManager resourceManager = new ResourceManager("MyApplication.MyServerApp.Resources.Messages", this.GetType().Assembly);
resourceManager.GetString("ResourceText" + MessageType + "Subject")

httpcontext.current.server.mappath Object reference not set to an instance of an object

I am using the following code within a class:
string filePath = HttpContext.Current.Server.MapPath("~/email/teste.html");
The file teste.html is in the folder
But when it will open the file the following error is being generated:
Object reference not set to an instance of an object.
Don't use Server.MapPath. It's slow. Use this instead, HttpRuntime.AppDomainAppPath. As long as your web site is running, this property is always available to you.
Then use it like this:
string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "email/teste.html");
if the code is not running from within a thread is executing a httprequest then HttpContext.Current is null (for example when you method is called via BeginInvoke) - see http://forums.asp.net/t/1131004.aspx/1 .
You can always use HttpRuntime see http://msdn.microsoft.com/en-us/library/system.web.httpruntime.aspx
If there is no HttpContext (e.g. when the method is called via BeginInvoke, as Yahia pointed out), the call to HttpContext.Current.Server.MapPath() must fail. For those scenarios, there's HostingEnvironment.MapPath() in the System.Web.Hosting namespace.
string filePath = HostingEnvironment.MapPath("~/email/teste.html");
You can use something like the following piece of code. One thing to note is that I was facing an issue, where I was trying to access a .txt file from within a TestMethod and everything was failing except for this...and yeah it works for non-Unit Test Scenarios too.
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,#"..\..") + "\\email\\teste.html";
Issue: I had an "Images" folder inside a class library project. But using the above answers, I was not able to get the physical path of the folder to read/write the files inside that folder.
Solution: The below code worked for me to get a physical path in the class library project.
string physicalPath = System.IO.Path.GetFullPath("..\\..\\Images");
I hope, it will help someone who is facing the same issue as me.

StringTemplate: Loading a Template from disk?

I am using StringTemplate in c# and following code to load a template from a subdirectory of my application.
StringTemplateGroup group = new StringTemplateGroup("myGroup", "/tmp");
StringTemplate query = group.GetInstanceOf("Sample");
query.SetAttribute("column", "name");
Console.WriteLine(query);
I have a template file Sample.st in the tmp directory of my application.
I am getting the following error.
Unhandled Exception:
System.ArgumentException: Can't find
template Sample.st; group hierarchy is
[myGroup]
Does anyone know what is wrong here?
Probably you should specify absolute path as a second parameter for StringTemplateGroup constructor?
In addition to adding a reference to the Antlr3.StringTemplate assembly, you ALSO need to add a reference to the Antlr3.Runtime assembly (not necessarily the Antlr3.Runtime.Debug assembly, although this would also work). This worked for me.

Getting the SourceDir property from a C# custom action

I have some directories that are bundled with my installer and I need to access them from within a custom action. I have done some research and seen that the SourceDir can be used to obtain the currently executing dir location. However I cannot find any examples of how to obtain this property? Or another way to obtain the current directory?
Can anyone advise or point me to anything other than the unhelpful Microsoft site?
I'm assuming you're using vbscript for the custom action. If so, properties can be accessed via the Session object. See below:
strSourceDir = Session.Property("SourceDir")
Be aware that the SourceDir property is only available at specific times during the installation.
For C#, you'll find that you can do something like this:
[CustomAction]
public static ActionResult MyCustomAction(Session session)
{
string sourceDir = session["SourceDir"];
string path = Path.Combine(sourceDir, "yourfilename.txt");
...
The documentation on MSDN is unfortunately lacking in making this clear.
As w4g3n3r mentions in his answer, SourceDir is only available to you at certain times. In short, you will need to make sure your custom action is called after a call to the ResolveSource action, which can only be called after CostInitialize has run.
Once SourceDir is set, it should be available for use for the remainder of the installation process.
Are you using InstallShield? Here's an example for an InstallScript CA:
MsiGetProperty(hMSI, "CustomActionData", strDirectory, numBuffer);
... where you also used a Set Property "Type 51" custom action to setup CustomActionData for your function to the value SOURCEDIR.

Categories