Asp.net mvc3 localization - linking resource with view? - asp.net

I use the localization paradigm of one resx file per view in all my projects. I have two identical folder structures for Resources and Views and I link the resource and the view like so:
#using Res = [Namespace].Resources.[Controller].[View]
<p>#Res.[ResourceKey]</p>
Is it possible to get asp.net to use/link the resource file without the using statements and defining a Custom Tool Namespace for all my Resx files? (Yes, I am that lazy.)

Open your web.config in your Views folder.
Add the namespace for your resources.
Restart VS as it doesn't pick up the namespace so it's not showing intellisense.
Call your resources from any view without adding using statements inside your *.cshtml files.

Related

ASP MVC 5 - Partial View File Outside of Views Folder

I have a partial view file which receives a #model saved outside of the views directory - actually stored in app_data.
However I get error:
The name 'model' does not exist in the current context
Source File: ...\app_data\_DefaultLayout.cshtml
Below is my code:
#Html.Partial("~/app_data/_DefaultLayout.cshtml"), Model)
Default Layout is as follows:
#using System.Web.Mvc
#using System.Web.Mvc.Html
#model MyApp.ViewModels.CreateCaseViewModel
// some html and razor code
This exception is a compilation error on the cshtml file. The view is not having access to model type. If you check your Solution Explorer, there are two web.config files, one in the Views folder and one at root level.
One way to sort it is making a copy of Views/web.config into App_Data.
App_Data is a special directory in ASP.NET, which is supposed to store data files like XML files / mdf files to store data. It has restricted access as well.
You should not be putting any UI layer concerns( views) in that folder. Asp.NET MVC has a convention for your UI layers, which is ~/Views directory. So use that location to keep your views.
If absolutely needed, you can put your view files pretty much anywhere in the application (Except those system directories), For example, you can create a directory called MyPartialViews and put your partial view there and refer the full path to that. The important thing to remember is, you need to have a web config in that folder (copy the one from the ~/Views and put it there). The web config has many needed elements in that including pageBaseType for all views

two problems with Web Site type project (not Web Application)

I have a project which is a Web Site, not Web Application and have two problems because of that.
I have a custom control derived from BaseValidator which I had to put in the App_Code folder. Now on the page I want to register this control by
<%# Register tagname="mytagname" Namespace="PP" TagPrefix="dv2" %>
But when I get enter to this page, I have exception, because the src attribute is missing. I can't put in the src attr. path to the App_Code because I have another exception. How can I do that? This is a Web Site, so the sources are not compiled to one dll file, so I don't know what to add to the src attribute.
When I want to add to the Web Site a new project library, I have to compile it and manually copy the library dll file to the bin folder in the Web Site. But don't know, how can I debug this library with brakepoints?
This is my first time with Web Site project type. I always created the Web App.
1- Don't use tagname attribute here, tagname is suitable for UserControls.
http://msdn.microsoft.com/en-us/library/c76dd5k1.aspx
2- There is no any difference between using library in a web app or website. Just choose Add Reference from project node's context menu and select your class library from project tab.
Try creating a separate project for your CustomControls and reference it in your WebSite project by the namespace you use in your CutomControls project. Also check out this post for some information asp.net add custom control in website
Your second question, if you keep all of your projects in the same solution and just reference them to each other's projects, you will be able to debug and step into methods and set break points in any of the projects in your solution. If you don't want to do that, you have to copy the debug symbol files along with the .dll to the bin or point VS to where the necessary debug symbol files are located and then you will be able to step through the code in the .dll

ASP.NET MVC - Non-Embedded Resource FIle

I currently have a local resource file in an ASP.NET MVC project. The goal of this file is to let a non-programmer edit the text on the webpage. The problem is that the resource file is an embedded resource and compiled on ASP.NET MVC Deployment. This means they would have visual studio to change site copy. This is non-optimal :)
Is there a way to make use of resource files in ASP.NET MVC that would result in a .resx file on the server for an admin-type person to be able to edit?
The other option is to put this in a database and have some front end to edit it, but I would really like to avoid this option as its overly complex for just a few text fields on a small site.
Thanks!
Once way of doing so, is to make sure the resources aren't compiled.
When you add a resource file (e.g. TextResource.resx) you can access the resources in a type safe manner.
For example:
ViewBag.Message = Resources.TextResource.MyMessage;
After you add a resource file (*.resx), select it in the solution explorer and view its properties. Clear the "Custom Tool" property. By default it contains the value "GlobalResourceProxyGenerator". Just delete this value.
This has a downside, however as you can no longer access the resources in a type-safe manner. You'll have to load them using the GetGlobalResourceObject and GetLocalResourceObject methods of the current HttpContext.
For example:
ViewBag.Message = this.HttpContext.GetGlobalResourceObject("TextResource",
"Hello", CultureInfo.CurrentUICulture);
This way you'll be able to manually alter the resource files (*.resx). You could then opt to build an application which can read and modify the contents of the resource files, which are just XML files.
For example:
http://www.codeproject.com/KB/cs/Editing_a_ResourceFile.aspx

consuming classes from folder other than App_Code

I am creating a site wherein I have a folder which contains some .cs files. I want to access those classes in .aspx and .ascx files. I’ve created some properties in it, but when I create the object of the class I don’t find that property via IntelliSense.
How can I use and consume those properties from that .cs file?
The website will only compile code files that are in the App_Code folder or are codebehind files for referenced controls. There isn't a way to reference classes defined in code files outside of the App_Code folder.
If you compile those classes and put the resulting dll in your website's bin folder, then you can reference them. To do that, you'll need to add them to a Web Application project in Visual Studio. See Ian Robinson's WAP blog post for most details.
Just have those properties public and you'll see them.
If still no luck please post your code and also tell: can you create instance of the class without error? Can you access any other properties or methods?

Not able to create an instance of a class

In a Project I have different modules or folders.In a module I have a namespace called idsobject.I have class in this namespace.
In another CS fle in the same folder I'm trying to access this class.But i'm not able.while if put that cs file in aPP_code than i able to get that class.
how to access the class in another cs file within the same folder.
thanks in advance
In an ASP.NET web site project, *.cs files are only compiled and made available to the rest of the application if they're located in the App_Code folder, or a subfolder in App_Code, or if they are associated with a *.aspx as code behind. The architecture is intended to allow full server-side compilation. You can't just put a *.cs anywhere and expect IIS to be able to find it, compile it, and link it with the rest of the app.
With an ASP.NET web application, you can put *.cs files where ever you want, because they are compiled by Visual Studio using details that are kept in the project file.
I'm guessing that you're using a web site project, which is why it works when you put the file in App_Code.
First check the namespace. If you accessing the correct namespace then perhaps the class access is set to private.
You could give us some extra information.
This sounds strange. Putting a class in a file in a Web project with a .cs extension should work just fine no matter which folder the file is located.
The only thing I can suggest is to make sure that there is at least a
using idsobject;
line at the top of the code unit where you are trying to access the class. The default behaviour for ASP.NET Web forms is for no namespace to be defined, which can sometimes lead to confusion.
The class should be marked as public if you've tried to access it from another namespace.
If it doesn't help, please post here a problematic code snippet.

Resources