Finding my View in asp.net mvc - asp.net-mvc

My Views folder has gotten crazy big! I would like to reorganize it so that the Views folder contains a list of Modules, and then each Module folder contains its share of the View (Controller) folders that currently appear under Views folder.
But of course this means going into each of my controllers and editing every view-returning method the explicit location of its view.
So instead of Controller Orders.Index method just having this:
return View();
I have to edit it to return this:
return View("~/Views/Orders/Index.cshtml");
You can imagine the suck level that this exercise attains over 50 or so controllers.
Is there some way that I can setup a routing or something per controller that will tell that controller's methods to go find their views in a defined subfolder of the Views folder?

It can be done with the help of CustomViewEngine
Follow this post and i hope you can provide your own locations to locate the view template.
MVC provide way where we can easily provide list of path to be searched
Locate view
Once you add CustomViewEngine, register it in Application_Start() event and then you are done :)
Happy coding

You could fix that by implementing a custom RazorViewEngine, where you can specify the search path for the views per request, per controller and so on.

Related

Creating a navigation for entire website

HI I am just learning asp.net mvc 3 and I am tryng to create a common menu for my entire application.I understand I can do that in _Layout.cshtml file witch is the default template for every page.
Now I have looked into sections for adding a template insite _Layout.cshtml but from what I can gather I need to define the section in every view.
I already have the logic for accesing the data defined in a separate class.All I need is to call the method witch will return a Dictionary<string , List<string>> , and then display the data by looping into it.
Aldo I could probably do this directly inside the Index.cshtml file by using the razor syntax I believe there must be a better way
So is there a way to create a template that can then bee added inside the _Layout.cshtml?
Creating a section for the menu on each view could work but I think another approach could be easier to mantain. On your layout itself use #Html.Partial to render your menu. Then on that partial view you could have all kinds of operations, such as database access, in one single spot.
Here is an article on how to do exactly this:
http://techbrij.com/981/role-based-menu-asp-net-mvc
Was just looking into something similar. Hopefully partial views are your answer, this is a template you can re use and stick on any page. Information found here.

MVC 4 solution structure Partial View

Well since most of the tutorials and demos chooses to put the partial view in shared folder im asking for a better way to do this.
My problem:
I want to use partial views in order to create a dynamic interface with reusable views. As i can with usercontrols. Since there might be a lot of partial views i want to put them in a seperate folder than the shared folder.
example:
How it looks today:
[View]
[View.Home]
index.cshtml
[View.Shared]
_layout.cshtml
A better way
[View]
[View.Home]
index.cshtml
[View.Shared]
_layout.cshtml
[View.Shared.Partial]
partial1.cshtml
etc
Or should i rethink my approach enirely? if so how? if not what should i think about?
Here is an example of something I do,
Application Wide used partial views - I put them in my Shared directory.
Partial Views for a specific Controller - I put them in the Views/[ControllerName] Directory.

How to make module base structure for asp .net mvc 3 project?

I want to create module based structure, Like in zend framework. Suppose I have 2 controllers like student and teacher and I want to put all these in one folder say shchool. Same way I want the view files for each controller in school folder for e.g
For Controller:
D:\aspprojects\Project1\Project1\Controllers\School\TeacherController.cs
D:\aspprojects\Project1\Project1\Controllers\School\StudentController.cs
and for view files:
D:\aspprojects\Project1\Project1\Views\School\Teacher\all CRUD files(*.cshtml)
D:\aspprojects\Project1\Project1\Views\School\Student\all CRUD files(*.cshtml)
Current structure is like as below,
for Controllers:
D:\aspprojects\Project1\Project1\Controllers\TeacherController.cs
D:\aspprojects\Project1\Project1\Controllers\StudentController.cs
For view files
D:\aspprojects\Project1\Project1\Views\Teacher\all CRUD files(*.cshtml)
D:\aspprojects\Project1\Project1\Views\Student\all CRUD files(*.cshtml)
What changes do I need to do?
The problem you are facing is that MVC doesn't care what folder the controller is in. In fact, it doesn't even have to be in a folder called Controllers. MVC only looks for classnames with Controller in the name. Once compiled, the folder structure is lost, and as such, no way for the framework to know to look in a subfolder for a view, because that information is no longer present in the compiled code.
You can still do it, however.. but you can no longer rely on MVC to find your view files automatically, you will have to pass each view name directly.
This means you will have to do this:
return View("~/Views/School/Teacher/Index.cshtml");
Rather than this.
return View();
Another option is to use areas, which allows you to create a School area, and then you can create a teacher and student controllers within the school area.

ASP.Net MVC: same action name in different controllers

I have 2 controllers which are SearchController and SearchByStaffController respectively. They are very similar and both have an action with action name "Search". When I call View("Search") in their common super class, the confusion comes. Only the "Search" view with SearchController is rendered.
Does the MVC framework get only the first view that matches the name and ignore the rest?
I tried to pass the view path in View() and it worked. Would there be any side effect for doing so? I searched over the web and seems no one has done this before.
Thanks!
Does the MVC framework get only the first view that matches the name and ignore the rest?
Yes. The routing rules are aparsed (top to bottom) and when a rule is matched all end.
I tried to pass the view path in View() and it worked. Would there be any side effect for doing so? I searched over the web and seems no one has done this before.
You can but I don't like that because MVC is based on conventions. So, I see forcing the path of the view a way to broke a convention. Are you sure you can't simply create two routing rules for the two methods? So you can do something like this:
return RedirectToAction("Search", "Controller1");
and
return RedirectToAction("Search", "Controller2");
user932390,
mvc uses convention over configuration. this means that the 'search' view will have to be located in both the:
views/Search
and
views/SearchByStaff
folders respectively. the only way around this is to locate the search view under the views/shared folder, then the viewengine will find it there in both cases and use it (assuming they have the same model).

Changing ASP.Net MVC directory structure

I've been working with ASP.Net MVC (3) for some time now and i like it a lot. But one thing i find a bit annoying is having to browse between the controllers / views / model / script directory all the time. So i'm wondering if there's a way to tell MVC to look for the files in a different location?
Maybe someone can tell me how to simply group the files together by controller like:
Directory: /Membership
MembershipController
LogOnView
LogOnModel
RegisterView
RegisterModel
Kind regards
Olav
I know exactly what you're talking about. Here are the conditions where I find the default MVC folder structure to be onerous:
I'm using a model-per-view approach
My controller basically only works with that one particular view
I have some javascript that only pertains to that view
Why do I want to put each of these pieces in a different folder?
I create a folder for the view in the Views folder, so you have a folder ~/Views/MyEntityList (just like the traditional MVC approach), but I put everything that pertains to that component there:
~/Views/MyEntityList/
MyEntityListController.cs
MyEntityListModel.cs
MyEntityList.js
MyEntityList.aspx
I find this structure leads all the developers to keep views decoupled from one another. No special MVC configuration is required, except for allowing browsers to access the .js resources directly.
There are some architectural patterns where this might not be a good way to go. For a model-per-view approach (see Los Techies for more description) I really like this structure.
I think you need to get the Solution Navigator extensions via Power Tools update for VS 2010.
That way, you can display in the Solution Navigator, as opposed to the solution explorer, only the open files, for example. Makes it easier.
By the way, delete all the model folders and create a separate model project, eg:
MyApp.Domain
Any solution that is beyond basic will benefit from this.
As stated in the comments to your question, Areas will also reduce your navigation requirements.
The only "looking of files" going on is with views, everything else is just a convention, so if you want you could have:
Directory: /Membership
MembershipController
LogOnView
LogOnModel
RegisterView
RegisterModel
... but the views must be in ~/Views/Membership
It looks like you have to override some behavior in the view engine. You can See this question to get a better idea.
One way I can think of to achieve this is by writing your custom view engine. You can place all these below files in Controllers/Membership
MembershipController
LogOnView
LogOnModel
RegisterView
RegisterModel
Models will not be a problem you can simply change the namespace for the models, the only problem is with the views. For this write your custom view engine so that your mvc application knows the physical location of the view files as follows.
public class CustomViewEngine : RazorViewEngine
{
public CustomViewEngine()
{
ViewLocationFormats = new[]
{
"~/Controllers/{1}/{0}.cshtml",
};
}
}
In global.asax.cs add the ViewEngine in Application_Start() by including the following code
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CustomViewEngine());
You may also have to take care of various other factors like updating the Layout attribute depending on where you place the _Layout.cshtml.
In case you are using areas, add the AreaViewLocationFormats string array as well.
You can do further customization by overriding some of the methods like FileExists, CreateView, CreatePartialView.
Note: Do not forget to copy web.config in the views folder to the Membership controller. Otherwise application does not find the required mvc namespaces and it does not find the symbols like viewbag, model etc.

Resources