How to get resource strings from ASP.NET markup? - c#

Hi
I have an assembly called like X.Common.DLL. There is some resources files for multilanguage app. Let's say it Language.resx Language.en-US.resx....etc....
I have a web application which contains this above dll as reference...
So how can I use this resources file in my web applications markup side?
Text="<%$ Resources:Class, ResourceKey %>" is not valid because of "Class" name is in another assembly...

You can easily create a wrapper class that does something like this
public class ResourceWrapper
{
private ResourceManager resourceManager;
public ResourceWrapper()
{
resourceManager = new ResourceManager("Namespace.Common", Assembly.Load("x.common"))
}
public string String(string resourceKey)
{
return ResourceManager.GetString(resourceKey);
}
}
Finding the correct name for the first param to new ResourceManager(...) can be a bit tricky sometimes.
To make it easier for yourself you can call like this:
Assembly.Load("x.common").GetManifestResourceNames() and check the returned results.
If you create a static wrapper, you can make the resource calling code as simple as this:
<%= Resource.String("MyResourceKey") %>

You should reference the other assembly in web.config to expose its content in web forms.
http://msdn.microsoft.com/en-us/library/ms164642.aspx
Edit : more detailed answer due to comments under :
You should complete the pages/namespaces section of the webconfig like this :
<pages>
<namespaces>
...
<add namespace="My.Fully.Qualified.Namespace"/>
</namespaces>
</pages>
Of course the assembly which provides the namespaces should also be referenced (project references, web.config's section)
Then you should be able to write things like "<%= MyResx.MyEntry %>

Related

How to register custom control in Dotnetnuke 8

I have created a class file with content:
namespace Controls
{
public class RadioButtonListCustomer : RadioButtonList
{
protected override void RenderItem(System.Web.UI.WebControls.ListItemType itemType, int repeatIndex, System.Web.UI.WebControls.RepeatInfo repeatInfo, System.Web.UI.HtmlTextWriter writer)
{
writer.Write("<td>");
base.RenderItem(itemType, repeatIndex, repeatInfo, writer);
writer.Write("</td>");
}
}
}
Register controls.
<%# Register Assembly="DotNetNuke.Web" TagPrefix="ww" Namespace="Controls" %>
Call controls:
<ww:RadioButtonListCustomer ID="irblUsers" runat="server">
</ww:RadioButtonListCustomer>
RadioButtonListCustomer is not found, so when run application, i get a error:
System.Web.HttpParseException: Unknown server tag 'ww:RadioButtonListCustomer'. ---> System.Web.HttpException: Unknown server tag 'ww:RadioButtonListCustomer'.
Starting with DNN 8.x and later, the application is now precompiled, which means that items outside of app_code will not be automatically included inside of the DotNetNuke.Web assembly.
If you change your register tag to be the following
I believe it will be able to find your control. This is assuming that you have your control defined inside of the App_Code folder.
I would recommend for better reusability in the long term that you create your own assembly and deploy in that manner though, it makes things easier later.

Why is Extension Method Not Found in MVC4 Razor View?

Given the following string extension method
namespace JHS.ExtensionMethods
{
public static class StringExtensions
{
public static string ToUSAPhone(this String str)
{
return String.Format("{0:(###) ###-####}", Double.Parse(str));
}
}
}
A #using statement was added to the MVC4 Razor view
#using JHS.ExtensionMethods;
and the following string value calls the extension method
#Model.producer.phone.ToUSAPhone()
which results in the following error
'string' does not contain a definition for 'ToUSAPhone'
I also tried putting the namespace in the web.config of the /Views folder and receive the same error.
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization"/>
<add namespace="System.Web.Routing" />
<add namespace="JHS.ExtensionMethods"/>
</namespaces>
</pages>
I have verified the extension method works by putting the same call in a C# class
string test=producer.phone.ToUSAPhone();
It seems the reference to the extension method is not available in the MVC4 Razor view but I can't figure out why?
This happens if the type you are trying to use the extension method on is actually a dynamic. Check to see if the exception is being generated by the CSharp RuntimeBinder. If so, you can either use the method as a common or garden static method:
#StringExtensions.ToUSAPhone(Model.producer.phone)
Or you can cast the value to a string:
#(((string)Model.producer.phone).ToUSAPhone())
According to Eric Lippert (formerly of MSFT):
The reason behind the fact that dynamics do not support extension
types is because in regular, non-dynamic code extension methods work
by doing a full search of all the classes known to the compiler for a
static class that has an extension method that match. The search goes
in order based on the namespace nesting and available "using"
directives in each namespace.
That means that in order to get a dynamic extension method invocation
resolved correctly, somehow the DLR has to know at runtime what all
the namespace nestings and "using" directives were in your source
code. There is no mechanism handy for encoding all that information
into the call site.
It's not just if the type you're calling the extension method on is dynamic, but if anything in the expression is dynamic and not cast.
eg this is clearly dynamic:
#ViewBag.ToJSON()
But I first thought Mike's answer did not apply to me because I was doing this :
#(ViewBag.UserProfile.GetJSONProfile().ToJSON())
where ToJSON() is my extension method and GetJSONProfile() just returns object.
I was just spacing out and being stupid but wanted to mention this.
Build your project before adding your custom namespace for the extentions to your View.
There may be yet another trivial reason for this and it happened to me.
The file that I created my extension in had "Content" as value for Build Action property on VS file properties pane.
Switching it to "Compile" immediately fixed the issue, naturally...
i had the exact same problem with the same error message, but in my case in some weird way, i fixed it by putting the ";"
so what was
#{var subti = item.subtitle.Truncate(18)}
was fixed with the ;
#{var subti = item.subtitle.Truncate(18);}
this maybe could help someone

C# Can I make a namespace accessible everywhere in my dot net web site for code-behind and classes

I have an extension method for String that I want to be available on every code behind and class in my solution. The method is sitting in a particular namespace. I'd like everywhere to have access to that namespace without me having to include it in every class.
I've used the "namespaces" tag in the web config to successfully include it on every aspx page, but this does not make it accessible on code behind or elsewhere.
So, is there a similar way to include a namespace everywhere?
So, is there a similar way to include a namespace everywhere?
No, I am afraid that there isn't. If you place the extension method in some of the root namespaces then it will be in scope for the child namespaces. For example:
namespace Foo
{
public static class Extensions
{
public static void Go(this string value)
{
...
}
}
}
will be in scope inside all classes declared in Foo.* namespaces. So you might put the extension method in a root namespace which has the same name as your project and then it will be available everywhere because all classes are automatically generated in child namespaces (unless you change that).
Just put you extensions class into System namespace and it will be available for every String object.
namespace System
{
public static class Extensions
{
public static void M1(this string value)
{
...
}
}
}
No, you cannot.
The namespace section of web.config only provides those namespaces to the markup, but not code-behind.
<configuration>
<system.web>
<pages>
<namespaces>
<add namespace="System.Data" />
<add namespace="System.Text"/>
</namespaces>
</pages>
</system.web>
</configuration>
You must explicitly add namespaces via the using statement in code-behind or add to one of the already included namespaces like System, but that is not recommended.
You'll have put a using statement on every code file that intends to have access to the namespace, you can't "gloabally include."
Usually, it's good form to put your various utilities, static classes, and extension classes in a "Common" project that your other projects all have a dependency on. I've seen this pattern re-used a lot:
Using YourNameSpace.Common.Utilities;

How to define endpoint without web.config or httpModule?

I would like to make a RESTful app of HTTPhandlers without having to define every endpoint by making an entry in the web.config, i'd like the style of attaching attributes to a class constructor eg:
public class obj : IHttpHandler
{
[WebGet(UriTemplate = "/accounts/{id}")]
public obj(string id)
{
// this is just an eg, it worild normally include caching and
// a template system
String html = File.ReadAllText("/accounts/accounts.htm");
html.replace("id", id);
httpcontext.current.response.write(html)
}
}
instead of
<httpHandlers>
<clear />
<add verb="GET" path="/accounts/*" type="MyApp.obj" />
</httphandlers>
The way i'm doing it now i have 100's of endpoints in the web.config :( i'd rather define them in the class. And i don't want to make extra files (.asmx) either. I'd like an app of just .htm files with tokens and .cs files
Thanks!
You could automate the registration of the endpoints and so on, with a custom ServiceHost, which overrides the ApplyConfiguration() method, which then virtualizes the configuration so that it does not have to be in the web.config file.
Here's a starting point. It doesn't do exactly what you want, but it illustrates the concept of virtualizing the configuration.

How to access an object declared in Global.asax outside of the file?

I have declared an object in my global.asax file as follows.
<object Id="WFRuntime" RunAt="Server" Class="System.Workflow.Runtime.WorkflowRuntime" Scope="application">
While I'm able to access this object within the global.asax file methods, I'm not able to use it elsewhere in the asp.net web application. I'm using .net 3.5 framework.
Any directions?
Thanks,
Socratees.
First, ensure that your Global.asax file has a code-behind
<%# Application Codebehind="Global.asax.cs" Inherits="YourSite.Global" Language="C#" %>
If you don't have one, create a Global.asax.cs file. This must be in your App_Code directory if you have a Web Site instead of a Web Project. It must look something like this:
namespace YourSite {
public class Global : HttpApplication {
public System.Workflow.Runtime.WorkflowRuntime WFRuntime { get; set; }
}
}
That should make it so all pages in your site can reference ((Global)Context.Application).WFRuntime.
Alternatively, you could make the WFRuntime member static and then just use "Global.WFRuntime" throughout your site.

Categories