Controllers creating correct routes so both work properly - c#

I currently have 2 controllers in my project. My navigation bar uses #Html.ActionLinks and I have the actionlink pick the action and the controller. For some reason the actions are correct but it doesn’t always choose the controller. So im guessing this is something to do with my roureconfig? I been trying to make it choose the correct controller or having it choose ether one wth {controller}/{action} but still I haven’t had any luck
I have a PartsController and a OrderStatusController. When I click on my parts actionlinks they all work, but when I click on my orderstatus action link it goes “parts”, “OrderIndex” even tho I have it specifically saying “OrderStatus”, “OrderIndex” any idea why here are some of my configs I tried?
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { controller., action = "Index", id = UrlParameter.Optional }
//);
// OrderStatus/{action}/{id}
//routes.MapRoute(
// name: "OrderStatus",
// url: "{OrderStatus}/{action}/{id}",
// defaults: new { controller = "OrderStatus", action = "OrderIndex", id = UrlParameter.Optional }
//);
//// Parts/Index
//routes.MapRoute(
// name: "Parts_Index",
// url: "{Parts}/{Index}/{id}",
// defaults: new { controller = "Parts", action = "Index", id = UrlParameter.Optional }
//);
// Parts/Index (Home Page)
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Parts", action = "Index", id = UrlParameter.Optional }
);

Configure your OrderStatus route like this:
routes.MapRoute(
name: "OrderStatus",
url: "{controller}/{action}/{id}",
defaults: new { controller = "OrderStatus", action = "Index"}
);
It needs to be above of the default route since the MVC framework evaluates the routes from top to bottom (the order matters here) that is also why you get to the Parts view when trying order status..

Related

How To Set Default Action on Mvc Startup [duplicate]

How do I set Default Controller for my ASP.NET MVC 4 project without making it HomeController?
How should I setup a default Area when the application starts?
the best way is to change your route. The default route (defined in your App_Start) sets /Home/Index
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters*
new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
as the default landing page. You can change that to be any route you wish.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters*
new { controller = "Sales", action = "ProjectionReport",
id = UrlParameter.Optional }
);
Set below code in RouteConfig.cs in App_Start folder
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional });
}
IF still not working then do below steps
Second Way :
You simple follow below steps,
1) Right click on your Project
2) Select Properties
3) Select Web option and then Select Specific Page (Controller/View) and then set your login page
Here, Account is my controller and Login is my action method (saved in Account Controller)
Please take a look attached screenshot.
I didn't see this question answered:
How should I setup a default Area when the application starts?
So, here is how you can set up a default Area:
var route = routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
).DataTokens = new RouteValueDictionary(new { area = "MyArea" });
In case you have only one controller and you want to access every action on root you can skip controller name like this
routes.MapRoute(
"Default",
"{action}/{id}",
new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);

MVC Routing issue with same parameter different names

I have the following routings in my RoutingConfig.cs
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// Not working why?
routes.MapRoute(
name: "AdminLoginRequestUrl",
url: "{controller}/{action}/{requestUrl}",
defaults: new { controller = "Admin", action = "Login", requestUrl= UrlParameter.Optional }
);
The problem is that the second routing is not working
What do i miss here? does someone has any tips or Ideas MVC is abit new for me
You cannot create different route only by differentiating parameter name, both route present similar one. Also move your custom route above default one. You can try this
routes.MapRoute(
name: "AdminLoginRequestUrl",
url: "{controller}/{action}/route2/{requestUrl}",
defaults: new { controller = "Admin", action = "Login", requestUrl= UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
you can use anything insted of route2 to differentiate from default route
You cannot differ your routs by variable names. You can create more specific route like this:
routes.MapRoute(
name: "AdminLoginRequestUrl",
url: "Admin/{action}/{requestUrl}",
defaults: new { controller = "Admin", action = "Login", requestUrl= UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Note that the order of registration is significant.
Or you can rename your parameters to something that will be appropriate for both controllers and use that.
Consider using MapHttpAttributeRoutes(), you can read about it here: https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/1.
Edit: Changed link for better article.
Then you can use attributes to define more specific rules on your actions.
Example:
public class Admin : Controller
{
[Route("admin/{requestUrl}")]
public ActionResult Login(string requestUrl) { ... }
}

Routing root request to this controller and action in MVC

I have a URL request in the following format: http://localhost/activate?activationCode=X
I would like this request to be handled in the Home controller, by the Activate action.
I am not sure how to proceed. I have looked at RouteConfig.cs and see the way routes are defined here:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
But how can I make Home/Activate handle URL's of the following format? http://localhost/activate?activationCode=X
...so as to add a special case where activate proceeding the host name goes to the Home controller, and Activate action?
You need to create a specific route
routes.MapRoute(
name: "Activate",
url: "Activate/{id}",
defaults: new { controller = "Home", action = "Activate", id = UrlParameter.Optional }
);
and place this one before the default route.
In addition if you want http://localhost/activate/X rather than http://localhost/activate?activationCode=X, then change it to
routes.MapRoute(
name: "Activate",
url: "Activate/{activationCode}",
defaults: new { controller = "Home", action = "Activate", activationCode = UrlParameter.Optional }
);
and make the method in HomeController
public ActionResult Activate(string activationCode)

changed route map not working

I set my app's route map as below:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Admininstrator", action = "Login", id =
UrlParameter.Optional }
);
but after some while I just changed my mind to modify my controller name to admin as below:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Admin", action = "Login", id =
UrlParameter.Optional }
);
and rebuilt and published my app but I can't get to localhost/admin/login
and the previous route map works! localhost/administrator/login why?
The second route points to the AdminController, if that controller doesn't exist (check in the Controllers folder) it will throw an exception.
Remane your AdministratorController to AdminController
or
change the route to point back to the AdministratorController

How to set Default Controller in asp.net MVC 4 & MVC 5

How do I set Default Controller for my ASP.NET MVC 4 project without making it HomeController?
How should I setup a default Area when the application starts?
the best way is to change your route. The default route (defined in your App_Start) sets /Home/Index
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters*
new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
as the default landing page. You can change that to be any route you wish.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters*
new { controller = "Sales", action = "ProjectionReport",
id = UrlParameter.Optional }
);
Set below code in RouteConfig.cs in App_Start folder
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional });
}
IF still not working then do below steps
Second Way :
You simple follow below steps,
1) Right click on your Project
2) Select Properties
3) Select Web option and then Select Specific Page (Controller/View) and then set your login page
Here, Account is my controller and Login is my action method (saved in Account Controller)
Please take a look attached screenshot.
I didn't see this question answered:
How should I setup a default Area when the application starts?
So, here is how you can set up a default Area:
var route = routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
).DataTokens = new RouteValueDictionary(new { area = "MyArea" });
In case you have only one controller and you want to access every action on root you can skip controller name like this
routes.MapRoute(
"Default",
"{action}/{id}",
new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);

Categories

Resources