ASP.NET MVC Subcontrollers - asp.net-mvc

How can i create a sub-controller in ASP.NET MVC?
i.e:
In controller's directory i have a controller named Users.Fine.//GET:/Users/Index
Inside controlles folder i have a subfolder named Groups,inside that subfolder i have a directory name Account,and inside Account i have a controller named Group.
So,the URL should be:
GET:/Groups/Account/Index
Correct?
But it doenst work,cant find that URL.
It expects:GET:Groups/Index
Any ideias?

You should not have subfolders in a controller. You should have a Controller for Users and a controller for Groups.
In the Users controller you will have a action called index which will give you the /Users/Index view.
In the Groups controller you will have an action called index and an action called Account You then can access these via /Groups/Index and /Groups/Account`.
If you wanted to have more nesting then you can use Areas. An Area will allow you to have a full set of controllers for a "sub folder". For example you can create a area called Group. Then you will have a default "Home" controller and view which would act as the index and you could add new controllers and views for each "sub-subpage" i.e /Groups/Account where group is the area and account is a controller in that area.

Related

asp.net mvc 4 Controller within a contoller

Can I have a controller within a controller.
I already have a users controller and I now want to add a Roles controller, But i want to access the roles controller from within the users controller like so: /users/roles/index rather than creating another controller and accessing it via /roles/index
If you want to keep to a typical REST implementation then I would suggest avoiding that and assign a controller to each of your root models (users, roles etc.).
Alternatively, if you want the Roles to be accesible only through the user, as a nested resource (meaning that you want to show, update, delete only roles of a particular user) then you can define routes like this:
routes.MapRoute(
"RolesForUser", // Route name
"user/{userId}/{controller}/{action}/{id}",
new { controller = "roles", action = "index", id = UrlParameter.Optional}
);
So a link to user/3/roles/index will take you to the Index action of the Roles controller, for user 3.
If instead your intention is to hide the roles management behind a /user/ url then I would go a bit further and create an user (or admin) Area in your MVC project (this is as simple as right-clicking on the project in Visual Studio and selecting Add -> Area...)

how to check view associated with which controller ?

I just get assigned to a ASP MVC project.
It has lots of controllers and lots of views...
I am getting confused abt which view is associated with which method of a controller ?
How to check which controller and method is associated with view ?
or
for which controller view has been added ?
Right click from the view; select go to controller
If you want to know the view of a method in a controller, right click on the method name. That is if public ActionResult Index(string returnUrl) is your method of a controller, right click on the Index, then you can see the option Go To View. Click on that to go to the view of that method. Hope this helps..
Well,
ASP.NET MVC, follows a "convention over configuration" thumb rule.
so unless you have configured something special in your project, it should follow a convention.
The convention is, every controller action would have a view with the same name.
I.e. If we have a controller of name "Users" with a controller method,
public ActionResult MyView()
then the corresponding View would be named MyView.aspx or MyView.shtml or MyView.cshtml inside a folder named "Users".
hope this answers your question.
There is many way to create URL in MVC.
1 Configure Route in a file and assign controller name and action
2 you can check URL and find the Controller and action. ie. http://www.abc.com/customer/address In this Controller name is "customer" and action name is "address"
Using action name, you can find View name is define or not if there is no view name its means view name is similar with action name under controller name folder.

Asp.Net MVC 3 - How to add/execute another view file

MVC Razor,
I have a View folder that contains
\Student\Create
\Student\Index
\Student\Delete...etc (default generated files) .
I wanted to create another view for example
\Student\Apply.cshtml
When I try to execute from e.g. Index to Apply file it shows file cannot be found.
Inside my Index file:
#Html.ActionLink("Apply", "Apply", new { id = #Model.ID })
How do I make it work? or it is not possible to have other view files except for the default generated files..
Thank you
You need to create an action that returns that view.
That's why it's called ActionLink.
Views are never shown on their own; they can only be returned by controller actions.
Have you created the Apply action in the Student Controller?
An actionlink points to an action which then renders a view.

by default controller search view only in two folder

i want to controller to search for a view in more than two folder (by default controler search in Shared and second folder with its own name ) can we make controller to search in any third folder including above two also.
Custom View Engine
Write a custom view engine and provide all your paths that you want. Then register it in global.asax codebehind in Application_Start event handler.
Check this question on SO that will give you all the information you need:
Can I specify a custom location to "search for views" in ASP.NET MVC?

organizing admin related controllers and viewpages

admin section is at www.example.com/admin
so I have an admincontroller.
But I also have user admin related controllers, and configuration controllers, I want the url to be like:
www.example.com/admin -> adminController
www.example.com/admin/user/ -> adminUserController
www.example.com/admin/user/edit
www.example.com/admin/user/add
I hate the name AdminUserController, any suggestions?
View pages are organized like:
/views/admin
/views/admin/user/
so in just manually reference the view page like return View("~/views/admin/user/add");
What other options do I have?
You can use multiple areas in the same project (in MVC 2). You could have an admin area. This lets you organize all the controllers in a sensible way, and fixes the naming issue: You'd have a "UserController" in the "Admin" area.
There is no direct relation between the Controller/Views Name and physical location and your Route, you can control this in the Global.asax, If you have an AdminController you can define a Route like
example.com/Admin/Manage/
In your Global will be like :
routes.MapRoute(
"AdminSection",
"Admin/Manage/{action}/{id}",
new { controller = "AdminController", action = "Index", id = "" }
);
So when a Route like this example.com/Admin/Manage/ is entered you redirect to the desired controller and action, the name of the controller is not strictly the one on the route.
Hope it Helps.

Resources