I have a file, AdminController.cs, that currently holds the controllers for every action in the admin section of my site. Obviously, this is getting huge, and I'd like to delegate control to a different controller for each application.
For instance:
www.mysite.com/Admin/Car currently looks to AdminController.cs to decide what to do. So when a user adds a car, there is an ActionResult in AdminController.cs called AddCar(). I would like this instead to look to CarController.cs to find AddCar().
So I after some research, I added this to my RouteConfig file:
routes.MapRoute(
name: "/Admin/Car"
url: "{parent}/{controller}/{action}",
defaults: new { parent = "Admin", action = "Index" },
constraints: new { controller = "Car"}
);
I commented out the ActionResults related to 'Car' in AdminController, and added them to CarController.
However, I'm getting "The resource cannot be found." when I navigate to www.mysite.com/Admin/Car.
How can I use a different controller, but with the URL still in the Admin realm?
You could add a controller action 'Car' to the Admin controller which redirects to the relevant action in the Car controller. But, Areas are probably what you want.
Related
I have two files called FilmController. One in the controller folder which displays my database data, and one in a folder called API which allows users to view data in json format.
My question is, in my nav bar i have an action link item that linked to the controller folder film file before i created the API one. Now it doesn't no which one to target. Is there anyway to target a specific one.
<li>#Html.ActionLink("Films", "Index", "Film")</li>
I want this to direct to the controller/film file.
You cannot use the ActionLink helper to target a specific Controller class.
However you can create a second route definition in your RouteConfig.cs. Let this route point to another namespace. Then put your API code in this namespace:
routes.MapRoute(
"API",
"api/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "MyMvcApp.Api" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
For those who have similar issue having two controllers with the same name in different folder, consider using Areas in your ASP NET MVC project and put each of these controllers to a different area. Then if you use #Html.ActionLink("Name", "Action", "Controller") in your view it will always choose the controller based on the area you are in, and if you want to have a link to a controller from another area, you can use #Html.ActionLink("Name", "Action", "Controller", new { area = "AreaName" }, null).
Sounds like you are using a standard controller as an api controler. One should be of type Controller, and the other ApiController, then they can both exist with the same name. #Html.ActionLink will only route to Controllers.
public class FilmController : ApiController
I am building an application in .net mvc4 that is based on business promotion and sales store.
User on this web application would be able to use product and access his/her personal business page also, that page can be promoted in future.
So I added one controller- Mypanel and a view of user's personal or professional business page _Mypanel.
Now the url access to this page is Bizcopter.com/Mypanel/_Mypanel
I want a custom user defined page name-
i.e. If a business name is - BookStore
Then I want to add a view in this same controller with the name of BookStore, So URL of personal business page would be-
Bizcopter.com/Mypanel/BookStore/ and this business holder can promote his business page with this URL.
Let me know if these are possible-
Replacing the view's name of user's choice
Add a view from client side in this same controller
I don't have any idea how to make it happen so don't have any trying code.
Site URL- http://bizcopter.com/Mypanel/_Mypanel
You don't need a separate view and controller action for each business.
I would create a controller and view called MyPanel. The controller takes a parameter called something like businessName that will load data related to the parameter.
By default you'll have a route in your AppStart/RouteConfig.cs which may look like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
The default URL structure might look something similar to: http://localhost:{PortNumber}/{Controller}/{Action}
Where we can attribute the following:
Controller = Home
Action = Index
Now if you want something similar to how you have it, you'd want something along the lines of:
http://testing.com/Fruits/Apples
Controller = Fruits
Action = Apples
By default, a URL pattern will match any URL that has the correct number of segments, in this case {controller}/{action}
Overall you should just need the MyPanel controller, and a controller taking a parameter of string which loads the correct Object/Model into the view.
Source: Pro ASP.NET MVC 4 - Adam Freeman
As the others said you can add a new route.Consider this code:
routes.MapRoute(
name: "MyCustomRoute",
url: "MyPanel/{name}"
defaults: new { controller = "MyPanel", action = "MyAction", name="" }
);
In this route if user type this URL:
Bizcopter.com/Mypanel/
Then it goes to your MyAction in your MyPanel Controller by default.Actually it will always go to MyAction, and in your MyAction, you must take the name parameter and redirect to user to the Relevant Action like this:
public ActionResult MyAction()
{
var name = RouteDate.Values["name"];
// check the name and redirect user to another action if it necessary
if(name == "BookStore") return RedirectToAction("BookStore","Mypanel");
}
I currently have 2 controllers, MemberController and Admincontroller, and is working fine if I use it like the below (different actions) :
http://localhost/member/delete/ME222
http://localhost/admin/view/AD321
I have my route config which looks like this :
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index", id = UrlParameter.Optional }
);
But now I have created a shared action (Detail) for both Member and Admin, which I put in my SharedController, and want to access it like so :
http://localhost/member/detail/ME222
http://localhost/admin/detail/AD321
Ofcourse when I hit the above url's, the action does not exist in the Admin- and MemberController.
How do I route the the above to go to the SharedController's action if the current current action in the controller (member or admin) does not exist? (not just the Detail action, but for all actions that doesn't exist)
Thanks
David
If you have common actions for both controllers you may simply create UserController which will be base class for Member and Admin controllers. Then you may put all common actions inside UserController and it should work.
If you want to override something (or for example put mark actions with different attributes for each role) you may make action virtual and then override it in child class.
You will need to make sure the action always exists... perhaps you can make a base class for your controllers.
Then inside the base action you can simply return:
this.RedirectToAction("action", "controller");
Pointing this at the relevant action on the shared controller.
So I am creating a site with two bindings
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "DefaultAdmin",
url: "Admin/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
The idea is I want to have EndUsers to access the normal pages (/account/login etc etc ) but have a admin only portion of the site (with a different layout) for admin users.
The question is kinda two fold :-
In terms of controllers it looks like MVC just looks in the Controllers folder, is there a way to seperate out AdminControllers and Regular controllers to keep things organised?
I would like to have a separate "master view" appear for the admin than the regular, currently I'm just using _layout.cshtml from _start.cshtml but I'd like to be able to use _layout.cshtml and _adminLayout.cshtml without prefixing every view with the name of the view (if not then I can live with this one easily enough).
Any help would be apprechited.
You can place controllers anywhere in the assembly as long as they are derived from System.Web.Mvc.Controller class they will be identified by the routing logic.
You could look at "Areas" in MVC to help out with your specific need.
It sounds like Areas in MVC is what you are looking for.
I code lots of ASP.NET but I'm kind of new with .net MVC, I've a default route registered like this:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
And I want to add another Administrator area on the site and all the URL would be something like "http://localhost/Administrator/controller1", "http://localhost/Administrator/controller2", etc. I've lot of controllers in the Administrator namespace and I'm trying to register those controller with only one MapRoute, I did something like this:
routes.MapRoute("Administrator_default", "Administrator/{controller}/{action}/{id}", new { controller = "Administrator", action = "Index", id = "" });
it works with those controller but one problem is that in some other controller while I try to do a redirect like:
return RedirectToAction("Index", "Forum");
Then I'll always be redirect to http://localhost/Administrator/Forum instead of http://localhost/Forum, it's not a big issue but make the URL looks strange, I tried to restrict to certain namespace but it's not working. It looks just as I'm trying to register two default route and .Net just match the first one, I'm wondering is there a way to make it two default route and map on only specific path only?
This exact issue is why Areas were added to MVC 2. http://www.asp.net/whitepapers/what-is-new-in-aspnet-mvc#_TOC3_2
Agree with Zach's answer.
Not ideal, but you do have the option to have controllers in the controller root folder (e.g. /controllers/HomeController.cs) of your project as well as the controllers in Areas (maybe high level root pages that display menus for areas).
Secondly a quick tip on using the RedirectToAction method. You can specify the area you would like to redirect too using the route parameters e.g:
RedirectToAction("Index","Form", new { area = "MyOtherArea" });