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) { ... }
}
Related
I am having an issue while dealing with routing in MVC.
I have defined the following routes in Route.Config
routes.MapRoute(
name: "Test",
url: "{controller}/{action}/{param}",
defaults: new { controller = "Home", action = "FirstAction" }
);
routes.MapRoute(
name: "Testy",
url: "{controller}/{action}/{secondparm}",
defaults: new { controller = "Home", action = "SecondAction" }
);
routes.MapRoute(
name: "Test2",
url: "{controller}/{action}/{encodedparam}",
defaults: new { controller = "User", action = "UserInfo" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Problem is that the first route is working fine but in second and third I got null values for the respective parameter.
Did I miss something?
Thanks in advance
You must match the parameter name for the third. If you write :
{id}
You must write
public ActionResult AnyAction(int id)
id could be of any type
i think there is conflict beetween your routes. You need only this mapping :
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
if you need custom routing for other elements don't put "{controller}/{action}/..." inside, because he will take the first route config that is matching with.
You can call all your routes precising the name of your parameter if its different from "id" :
http://localhost/home/firstaction?param=123
http://localhost/home/secondaction?secondparam=123
http://localhost/user/userinfo?encodedparam=123
As you want to use the URL in such way: localhost:portnumber/home/secondaction/value instead of localhost:portnumber/home/secondaction?secondparam=value.
You have to follow the below approach, which would behave as generic for all your Action Methods, whether they are containing parameters or not.
Declare default route in RouterConfig.cs as:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Then Parameter name in method must be same as in default route:
public ActionResult SecondAction(string id)
{
return View();
}
Paramter type could be any type.
In this way you do not need to declare other routes.
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..
I tried to use map route on my mvc project,first route "route1" is working fine with two parameters
http://localhost:43601/records/index/1/5454546
now the second route - on the same controller - recordcontroller- is NOT working "route2"
http://localhost:43601/records/attachmentdetails/828/2
and gave an error:
The parameters dictionary contains a null entry for parameter 'attId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult AttachmentDetails(Int32, Int32)'
any help please?
//route code
routes.LowercaseUrls = true;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//Web API
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "route1",
url: "{controller}/{action}/{libId}/{recordNo}",
defaults: new { controller = "Records", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "route2",
url: "{controller}/{action}/{attId}/{atype}",
defaults: new { controller = "Records", action = "AttachmentDetails", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default4",
url: "{controller}/{action}/{attId}/{atype}",
defaults: new { controller = "FileManager", action = "BookAttachmnt", id = UrlParameter.Optional }
);
//controllers code
public ActionResult Index(string libId, int recordNo = 0)
{
}
public ActionResult AttachmentDetails(int attId, int atype)
{
BasicSearchAttribute();
return View();
}
You are matching route1 when you really want to match route2.
To achieve this, use this route instead, and put it before route1.
routes.MapRoute(
name: "route2",
url: "Records/{action}/{attId}/{atype}",
defaults: new { controller = "Records", action = "AttachmentDetails", id = UrlParameter.Optional }
);
The reason your code isn't working is that routes are matched 'top down' (i.e. the order in which they are defined). The URL you are using meets the rules for route1 before it meets the rules for route2. Alas, route1 has different parameter names (libId rather than attId) and thus the routing fails since your action wants a attId parameter, but was given a libId parameter.
But putting the above route first it will mean that it gets used, rather than route1. Note also that I have hard-coded Records in the route, to ensure that URLs that start with Records are handled by route2 and everything else is handled by route1 (or later routes).
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)
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