How to change default view creation logic in visual studio? - visual-studio-2013

I've looked here and here to try to answer this question as well as a billion Google searches, but have yet to come up with a solution.
My directory structure is a multi-tenant MVC site with an overridden view engine that uses Areas to serve content for different hosts that share a lot of common logic served globally from the base controllers and views folders. We have taken this a step further and broken out different global site sections into a new folder called SiteSections. Inside of this folder we have more Areas.
The issue I am having, is whenever I try to use the visual studio context menu from inside a controller that is inside the SiteSections folder, it always adds it to the global Views folder.
What I am assuming is happening is that since these are Areas held within a different directory, Visual Studio is searching the Areas folder for an Area with the name of which I am working in. Since this is kept in a different directory, it is just defaulting to the global one. I've looked into all the different T4 templates and do not see anything specifying the directory where the view will be created.
I have just one question, that I'm hoping is possible.
How can I override Visual Studio to look in a second directory for the Area in question?
Thanks in advance!

I experienced something like that, not with Areas, but with Folders.
Have you tried to add custom view engine on ViewEngines?
The steps that i followed:
1 - I put this line at Global.asax.cs on method Application_Start:
ViewsEngines.Engines.Add(new MyCustomViewEngine());
2 - I created the file named as MyCustomViewEngine inheriting from RazorViewEngine, for example:
public class MyCustomViewEngine : RazorViewEngine
{
public MyCustomViewEngine()
{
base.ViewLocationFormats = MyViewLocationFormats;
}
private static string[] MyViewLocationFormats = new[]
{
"~/Views/Folder/{0}.cshtml",
"~/Views/Folder_1/Folder_2/{0}.cshtml"
}
}
I think that you can use in this way: "~/Areas/Views/Folder/{0}.cshtml",
Hope this helps!

Related

view files inside public, is it BIG security risk?

I am looking at a laravel theme package that stores the themes with assets and views inside public folder. This package is developed by a laravel pro so I wonder why he decided to store view files of the theme inside public folder? Some other theme packages split theme between public and resources/views folder.
If I use this package, which has nice features, what could happen to my website? Could it easily get exploited or hacked if view files are inside public folder? Is there 100% way to secure the website that has view files in public folder? What is that way?
Can you give me your opinion and some examples what could happen with this approach?
IMO this is kind of rubbish. It let any user see your views. There shouldn't be any logic in those, but it can.
Also, /public isn't the right place to store view. They should be in /resources/view therefor in dont see any reason why they shouldn't be in the right directory
of course it is .. it's like telling the whole world that "hey this is my php script".. this is not the proper way of doing such so..

How to prevent a new controller from creating a new folder for its views in the Views folder?

I'm writing an ASP.NET MVC 5 application, where folders are organized by feature (ViewModels, Controllers, and Views are grouped together). I was able to override all default folder paths and manage all routing successfully. However, every time I create a new controller, Visual Studio creates a new Views folder and a child folder with controller name, which is a little bit annoying but that's fine.
Now, when I create another controller, I'm getting this error, because Visual Studio created the Views folder again, which is hidden now (excluded from the project). To fix this, I have to include the Views folder in the project and delete it after I create the controller.
Does anybody know any way to prevent this?
Error
There was an error running the selected code generator: 'A file or
folder with the name 'Views' already exists on disk at this location.
Please choose another name.
If this file or folder does not appear in the Solution Explorer, then
it is not currently part of your project. To view files which exist on
disk, but are not in the project, select Show All Files from the
Project menu.
Instead of using visual studio's Add -> Controller which generates the undesired view folder, create your controller using Add -> Class. Name the class conventionally with a Controller suffix and subclass Controller like this MyController : Controller.

How to share image files across projects in one solution in visual studio 2012?

I have a solution with multiple projects in it.
And I have lots of pictures which two or three projects would need them.
How can I add these images once and share them across all the projects in the solution?
Really depends on what you want.
You could use linked files.
To do so simply have one "common" project with the resources and in the second project you use "Add Existing Item", before you click add, click the small arrow right to the button and say "linked file". This will not copy the file but keeps it in the current location. But it will treed it as if it is a normal project file...
If you just need static content shared across multiple web sites,
a) have one web site storing the static content and reference all URLs to link to that static content web site.
b) If you don't want to host a separated web site for the static content, simply create a sub website within each main web site...
If you have static content, create a virtual directory within your web site linking to a folder with all your static content.
Write a dll to hold all the resources and make them public, or my preference would be to add some methods to the dll to access named resources, e.g. public Stream GetImage("logo"), then reference it from your other projects.
Would be a couple of ways.

ASP.NET MVC3 Using Areas

I'm having some troubles setting up two Areas in my project, I created both Areas by right clicking my project -> Add -> Area using VS 2010 Professional,
The code it generated in the *AreaRegistration.cs files is exactly as I'd expect it.
Both my areas have multiple controllers.
I've checked my global.asax.cs file and it contains what I'd expect, the following is at the bottom:
// Generated code
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
When I change the default generated routing info I can get it to work for a Single controller:
context.MapRoute(
"CommunityCourses_default",
"CommunityCourses/{controller}/{action}/{id}",
new {controller="NameOfController", action = "Index", id = UrlParameter.Optional }
);
The controller="NameOfController" is what I've tried adding to the generated code, but it only works for a single controller.
Edit: I renamed the namespace of my files to use PROJECTNAME.AREANAME within an area, is this causing issues? I thought it'd simplify my imports in a non-convention-breaking way.
What am I missing? I've noticed a lot of problems about areas on SO and tried to follow the answers without success. This issue has bothered eluded me for the majority of the day, any help would be greatly appreciated!
If you moved files from the Controllers folder or the VIews folder in the root of the project into Controllers or View folders contained in the {AreaName} folder, then all of those files moved need their namespaces changed from {ProjectName}.{*etCetera} to:
{ProjectName}.Areas.{AreaName}.{*etCetera}
Turns out what was wrong was that the order or routing was incorrect, it was processing {controller}/{action}/{id} first which was breaking my areas. Moving the
AreaRegistration.RegisterAllAreas();
to the top of my application_start fixed my problem.
http://haacked.com/archive/2011/04/13/routedebugger-2.aspx Link to the tool I used to find the problem, unfortunately the current version of the tool doesn't work on the default 404 pages, so I also had to create a custom 404 page for it to work.

Visual Studio 2010: Folding auto-generated resources behind other files?

I have the following file: Foo.cs with a Foo class inside of it. I can't seem to find the right way to keep my resource files (resx) organized behind their respective files.
If I create a Foo.resx the resource file gets folded away nice and tidy behind the Foo.cs class. This, however, causes issues because the standard custom-tool that generates the code attempts to create another Foo class (Look at the Foo.Designer.cs: internal class Foo { ... }). If my Foo.cs file does not already contain a Foo class, this works fine (no naming collision).
To fix the naming collision I attempted to give it a custom namespace MyProj.Resources and use an alias to identify it: using R = MyProj.Resources.Foo; This still causes issues because the auto-generator has an issue creating a ResourceManager properly.
If I, instead, name it something along the lines of FooResx.resx it does not automatically get folded behind the Foo.cs file. Instead, it resides in the solution explorer right below it. Going into the MSBuild (.csproj) file and adding a <DependentUpon> tag, then Visual Studio neatly tucks away my FooResx.resx file. However, I can't actually use any of the resources from that file because the auto-generated code has an issue creating a ResourceManager properly.
Basically, is there any way to have the Resource files (resx) fold behind a cs file and still work properly using the standard Custom Tool (ResXFileCodeGenerator)?
I do realize that I can always place all my resources into a file within the properties folder: resources.resx. I'm trying to organize them better than that though.
Update:
I decided to manually edit the auto-generated code and make it partial. This allowed the code to compile, but I still ran into the same issue (Issue #2). It seems that if a resource file is folded behind (manually or automatically) another code file then the ResourceManager has trouble finding the *.resource file. This might be an issue I'll have to raise with Microsoft Connect about the ResXFileCodeGenerator tool. It needs to be able to locate the proper *.resource file when folded behind other files.
The solution could be to make your classes and your generated code partial classes - if you look at a .Designer.cs (from a System.Windows.Forms.Form for example) you will discover that it declares something like partial class Foo.
Foo.cs
public partial class Foo
{
}
Foo.Designer.cs
partial class Foo
{
}
Edit
It turns out that StronglyTypedResourceBuilder or PublicResXFileCodeGenerator insists on generating classes with either internal or public access modifier (it can be set in the .resx).
Setting ResXFileCodeGenerator as the CustomTool in the properties of your .resx still doesn't give you the behaviour you'd see in a generated .Desinger.cs of a Form.

Resources