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.
Related
I have an issue in Routing in MFA where link like
Project_Name\Controller\Index is working, whereas
Project_Name\Controller\ is not working
This is happening for only some controllers after being deployed in a server.
I am getting the following error:
403 - Forbidden: Access is denied.You do not have permission to view this directory or page using the credentials that you supplied.
Is there any further configuration values that needs to be considered?
In RouteConfig we define the route path like this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
So the controller you are using may have Index action method and the URL works.
But if your controller does not contain Index method or it is having parameters then it will not work.
This is a possible solution that you might create an action method named Index in all those controllers.
for more, you need to share some more details.
I have one solution with two project MVC. I added a references to second project to main project. I create a new MapRoute in first project to second project.
routes.MapRoute(
name: "second",
url: "second",
defaults: new { controller = "Home", action = "Index" },
namespaces: new[] { #"SecondProject.Controllers" });
this code has been added in RouteConfig in main project
when I write in URL address https://localhost/second... not working...any idea why?
First up all please check the namespace path is right at your end. then add below line in route config after route declaration.
myRoute.DataTokens["UseNamespaceFallback"] = false;
you can refer stackoverflow link as well which may be helpful
Controller in separate assembly and routing
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 currently have 2 projects in same folder.
Main
Project1
Project2
Problem:
Multiple types were found that match the controller named Account. This can happen if the route that services this request ('{controller}/{action}/{id}') 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 Account has found the following matching controllers:
Project1.Controllers.AccountController
Project2.Controllers.AccountController
I using Foundation 4.
Thanks advance
you need to use the version with this signature
public static Route MapRoute(
this RouteCollection routes,
string name,
string url,
Object defaults,
string[] namespaces )
To make it work just set up two almost identical routes - one which includes the project1 for the namespaces parameter and then a second identical version but use project2 in the namespaces parameter. That said, it would generally be less confusing, to use different names if you can...
routes.MapRoute(
name: "Default_Project1",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional,
namespaces: new string[] { "Project1" } }
);
routes.MapRoute(
name: "Default_Project2",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional,
namespaces: new string[] { "Project2" }
);
I had the same problem when I copied an MVC project and renamed all the namespaces.
I tried the above solution but it did not work.
Visual Studio was still looking at the old namespace dll's even after I cleaned and rebuilt my project.
So the solution:
Right-click solution, click 'Clean Solution'
Manually delete everything in the /bin and /obj folders
Build your solution
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" });