changing App_Start to AppStart - asp.net-mvc

I have set up a new MVC 4 web application. I am currently running source code anaylsis against the project and getting the following error:
CA1707 Identifiers should not contain underscores
The reason for this is because of the namespace "App_Start" due to the App_Start folder name. Are there any issues with renaming the folder to "AppStart"? This will mean all my rules are passed.

I would suppress that rule for that particular namespace rather than rename it. Create a GlobalSuppressions.cs file in your web project root, and add this:
using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Microsoft.Naming",
"CA1707:IdentifiersShouldNotContainUnderscores",
Scope = "namespace",
Target = "<namespace>.App_Start",
Justification = "This is an infrastructure namespace")]

Related

MVC 4 error in accessing resource file from controller

Getting error in accessing resource file string from MVC controller(working file with views)
Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "TempNameSpace.App_GlobalResources.Resource_EN_US.resources" was correctly embedded or linked into assembly "TempNameSpace" at compile time, or that all the satellite assemblies required are loadable and fully signed.
What I have already done
1. Resource file build action : from content to Embedded resource
Result : Publish does not include resource files in it.
2. Added new resource file with name Resource.resx
Result : Same error
Solution hierarchy
1. Solution contains 4 projects, two with resource files(Resource.resx and Resource_EN_US under App_GlobalResources folder)
2. Designer for Resource_EN_US contains correct namespace
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("TempNameSpace.App_GlobalResources.Resource_EN_US", typeof(Resource_EN_US).Assembly);
In my projects I use a seperate project for resources. I have set all resource files Build Action property to Embedded Resource.
On the reference to the resources project, I have Copy Local set to True for the resources project. (Rightclick a reference and select Properties)
If you have your resource files inside your web project, I think that the property corresponding with Copy Local should be Copy to Output Directory (Also accessible via rightclick and Properties).
Edit:
I see that in your comment you said that you access the file as Resource.Resource_EN_US.WaterMarkEmail but this is not the right way to access the files. You should access them as one single file: Resources.Property.
My files are named as: Standard.resx, Standard.en.resx, Standard.no.resx and so on. To access them I can use Standard.Property. The resource files will mold toghether, but only if you give them valid names. If you can access all your resource files individually, then you have not used the correct naming convention.
I recommend implementing a ResourceHelper that uses the ResourceManager to access the property that you need.

ASP.NET MVC 4 bundle with wildcard in folder path

My team uses a custom NuGet package for installing jQuery UI, which puts the theme files into a directory structure like this:
Content
jquery-ui-1.10.3
images
jquery-ui.css
jquery-ui.min.css
I'm trying to use ASP.NET MVC 4 bundles to include this content in the BundleConfig class inside my application's App_Start folder like so:
bundles.Add( new StyleBundle( "~/bundles/css" )
.Include( "~/Content/normalize-{version}.css",
"~/Content/jquery-ui-{version}/jquery-ui.css",
"~/Content/Site.css" ) );
This throws an error when I run the site:
Directory does not exist.
Parameter name: directoryVirtualPath
I also tried:
bundles.Add( new StyleBundle( "~/bundles/css" )
.Include( "~/Content/normalize-{version}.css" )
.IncludeDirectory( "~/Content/jquery-ui-*", "*.css" )
.Include( "~/Content/Site.css" ) );
That doesn't work either (obviously). I can explicitly specify the version on the folder, but that defeats part of the benefit of using the bundle.
So how can I use a wildcard in the folder path?
You could use the overloaded version of IncludeDirectory which searches subdirectories.
Suppose you have the following file:
\Root\Content\jquery-ui-1.10.3\jquery-ui.css
Use this code to find and add it:
.IncludeDirectory("~/Content", "jquery-ui.css", true)
This is useful because it will always find jquery-ui.css, regardless of where you put it.
The downside to this method is that it will search for and include all jquery-ui.css files that it finds, which could cause some bugs if you don't ensure that only one jquery-ui.css exists.
(Remember that searching for subdirectories will also still search the root directory i.e. ~/Content)

Properties.Resources.XXXX Namespace or Module not Defined

I'm trying to read in the text (as a string) of an XML file from my Resources. The XML file is named MyXMLResourceFile.resx.
I tried using the C# way using
let myFile : string = Properties.Resources.MyXMLResourceFile
but it is giving me the error (under Properties):
The namespace or module 'Properties' is not defined.
I'm assuming I'm missing an open but have no idea what it would be.
Sorry, I'm still pretty new to F# coming from VB.
Additional information:
I made the resx file myself per this SO answer I then looked to see how others accessed it. In C# they did Properties.Resources.XXXX per this project I saw you could use ResourceManager but didn't see any method to read the whole file directly into a string.
The namespace or module 'Properties' is not defined.
That's because the namespace Properties is not defined. You probably brought the resx file from another project. Open the associated .Designer file, if it doesn't appear in the F# project, open it up in the original project. You will see a namespace with and a bunch of C# code, assuming the original project was in C#. The namespace of this file has to be the same namespace of the project where you are trying to call it.
But, since this is C# code, it won't run on a F# project. You have two choices: use a code generator to generate the associated .Designer class in F# code with the Properties namespace, (I don't know if such generator exists); or pre-compile the resx in the original project and reference it as a dll from your F# project, then you can access it with ResourceManager.

Resource namespace not exist if resource file named using culture prefix

I have a resource file named Res.en.resx in my Asp.Net Mvc 4 project.
In a view I'm trying to access its properties like this: #MvcApp.Resources.Res.Title but I'm getting the following error:
The type or namespace name 'Resources' does not exist in the namespace 'MvcApp' (are you missing an assembly reference?)
If I change resource name to Res.resx everything works fine.
Why is this happening?
I believe you need to have at least a file named xxx.resx which contain the translation for the default language of your application.
So if your main language is french and you would like to have an english version, you need:
xxx.resx file which contain the default language of your app.
xxx.en.resx file which contain the english version of your file.
Hope this help!

How do I publish/deploy an MVC Project with ResX Resources?

I have written a VERY simple MVC application which just displays a single string from a Resource file. It works just fine on my local machine but when I deploy the project to the web server I get the error.
CS0103: The name 'Resources' does not
exist in the current context
You can very easily replicate exactly what I am doing in just 10 steps!
Create a New MVC 2 Web Application.
(File->New->Project->ASP.NET MVC 2 Web Application, say no to the Unit Testing Project)
Add the "App_GlobalResources" folder.(right click the project and select Add->Add ASP.NET Folder->App_GlobalResources)
Add a Resource File to this folder.(right click the folder and select Add->New Item...->Resources File. Name it Strings.resx)
Add a single string to the Resource table.(Name = "HelloWorld", Value = "I localized Hello World!")
Set the File Properties for the Resource File.(Click the file Strings.resx and int the Properties window set Build Action = "Embedded Resource" and the CustomTool = "PublicResXFileCodeGenerator")
Add a new Controller(Right click the Controllers folder and select Add->Controller... Name it HelloWorldController.cs)
Add a the View(With the cursor in the Index method of the HelloWorldController.cs Press CTRL-M-V. Use the default values including View name = "Index")
Modify the View so that it displays our string from the resource file.Replace the content of the MainContent placeholder with
<h2><%: Resources.Strings.HelloWorld %></h2>
Run it locally to test that it works. Which it should.
Publish it to a web server and visit the url "http://localhost/HelloWorld"
This is where I see the error described at the top.
I would imagine that the settings I've put on the ResX file are incorrect and the resource is not published to the server.
Help is greatly appreciated.
Thanks!
Ah ha! Figured it out. In LARGE part to this article:
http://odetocode.com/Blogs/scott/archive/2009/07/16/resource-files-and-asp-net-mvc-projects.aspx
Sounds like the App_GlobalResources folder is NOT cooperative with MVC. So I moved my ResX file to a new folder~/Resources/Strings/Strings.resx
This along with 1 minor change to set the File Property
Custom Tool Namespace = Resources
and Problem Solved!

Resources