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
Related
I have some problems when I have same controller name in separate projects.
My main solution is Web forms, and I have two MVC separate projects(separate folder) , the problem If I have a controller in first project with name HomePage and same controller name in solution 2 I have an error:
Multiple types were found that match the controller named 'HomePage'. This can happen if the route that services this request ('{*pathInfo}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
The request for 'HomePage' has found the following matching controllers:
Project1.Controllers.HomePageController
Project2.Controllers.HomePageController
The global.asa is in the web forms solution which I added two routes map but I still have same error.
Any solution to fix this issue?
Can I can in the view the action with namespace
#Html.Action("index", "HomePage")
Thank you
If you need to set the namespaces value when registering the route, you should do so like this:
routes.MapRoute(
name: "RouteName",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "MyNamespace.Controllers" });
By specifying the namespace, it removes the ambiguity.
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.
I have created a controller with an Index action. All of my other Actions return the views just fine...but for some reason I have to specify the full url to have the Index view return. It's almost like my Routes aren't working correctly.
For instance, to go to the properties page, you have to go to /Properties/Index instead of just /Properties/. My routes are as follows. Any help would be greatly appreciated!
routes.MapRoute(
name: "Index",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
For security reasons in ASP.NET in general, you can't have a "Properties" path. C# projects all come with a Properties folder by default and ASP will ignore it when accessed directly to prevent file access to it.
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" });