Considering the mapped routes below, when the user goes to /Home, why would the second route be hitting instead of the first? My assumption was that going to /Home would result in the first route being hit, but for some reason the second one is being used.
Why is this?
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Home page behind auth",
"Home",
new { controller = "Home", action = "HomeSecure", id = "" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
}
edit: just to be clear, I want /Home to go to the HomeSecure action in the Home controller.
edit2:
public ActionResult Index()
{
return View();
}
[AuthorizeHasAccess]
public ActionResult HomeSecure()
{
return View();
}
Given the information that you've given, I don't think that either route will match the request. MVC will be looking for method signatures with an "id" argument, and your action methods have none. If the request does in fact take you to the Index method on the Home controller, check to see if an error handler somewhere is trapping that condition and then redirecting you to /Home/Index
If you set the route table as follows, the first route will match as required.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Home page behind eRaider",
url: "Home",
defaults: new { controller = "Home", action = "HomeSecure" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", UrlParameter.Optional }
);
}
Related
I am using ASP.NET MVC5, my routing was working fine until i create new controller Administrator with Action CreateRole. previously i have modified routes in RouteConfig for my Dashboard, as i don't want to take controller name but just Action name. I have tried with Administrator controller but i still getting error "The resource cannot be found."
many thanks in advance
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//======In order to use only Action Title in URL======//
//---Dashboard Home Route--//
routes.MapRoute(
"Home",
"Home/{id}",
new { controller = "Dashboard", action = "Home", id = UrlParameter.Optional }
);
//**************************Administrator Route***************************//
routes.MapRoute(
"CreateRole",
"CreateRole/{id}",
new { controller = "Administrator", action = "CreateRole", id = UrlParameter.Optional }
);
//========default routing=======//
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
}
Controller
public class AdministratorController : Controller
{
public ActionResult CreateRole()
{
return View();
}
}
I have a controller named Registration and an action method in it as the following:
public JsonResult GetReqs(GridSettings gridSettings, int rt)
{
...//whatever
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
So I added a route and now my RouteConfig.cs is like this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "RegReq",
url: "{Registration}/{GetReqs}/{rt}",
defaults: new { controller = "Registration", action = "GetReqs", rt = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
However I can't access the rt parameter and the action method GetReqs isn't called(I set a breakpoint on it but nothing happened). Where is the mistake?
Edit: Link example I tried : ~/Registration/GetReqs/1
I think you need to remove the brackets in your first route:
routes.MapRoute(
name: "RegReq",
url: "Registration/GetReqs/{rt}",
defaults: new { controller = "Registration", action = "GetReqs",
rt = UrlParameter.Optional }
);
The default route has {controller} to tell MVC to use the string in that section of the url as the controller name. You know the controller, so you just need to match the specific string.
I am trying to show user details at the following url :
www.website.com/users/yasser
where the last entry yasser is the username I have tried a couple of routes but it just does nt work.
My User controller is as shown below.
public class UserController : Controller
{
public ActionResult Index(string username)
{
var model = _service.GetUserDetails(username);
return View(model);
}
}
I have reffered this and couple of other links, but I really could not figure out how it worked.
Can some one help me out on this. Thanks
Edit :
My current route config is below
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Routes executes from top to the bottom:
routes.MapRoute("UserProfile",
"Users/{username}",
new { controller = "User", action = "Index", username = string.Empty }
);
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);
I have a controller with an index action.
public ActionResult Index(int id = 0)
{
return view();
}
I wish to pass id into the index action, however it doesnt appear to work in the same way as the details action.
e.g. if I want to pass id 4 into the index action, I have to visit url:
http://localhost:8765/ControllerName/?id=4
With the details Action... I can do this.
http://localhost:8765/ControllerName/Details/4
What I want to do with Index is something like...
http://localhost:8765/ControllerName/4
When I visit this url, I get an error:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /fix/1
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
Is this possible? How can I get MVC to automatically treat the index action in the same way as the details one?
Thanks
UPDATE - MY CURRENT ROUTES CONFIG
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
UPDATE NEW RouteConfig Class still doesn't work when I visit localhost:1234/Fix/3
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "FixIndexWithParam",
url: "Fix/{id}",
defaults: new { controller = "Fix", action = "Index", id = UrlParameter.Optional });
}
}
Update It's worth pointing out, /ControllerName/Index/4 should work with the default route.
With the default route there, it expects the second parameter to be the controller name.
So with the Default Route /ControllerName/4 is being interpereted as ControllerNameController Action 4, which of course does not exist.
If you add
routes.MapRoute(
name: "IndexWithParam",
url: "{controller}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
before the default one it would allow
/Home/4 to be routed to HomeController action Index with id=4
I have't tested this, it may conflict with the default. You may need to specify the controller explicitly in the route, ie:
routes.MapRoute(
name: "HomeIndexWithParam",
url: "Home/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
(Obviously, replace Home with whatever controller you're actually wanting to route to)
I'm learning about creating custom routes in ASP.NET MVC and have hit a brick wall. In my Global.asax.cs file, I've added the following:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
// My Custom Route.
routes.MapRoute(
"User_Filter",
"home/filter/{name}",
new { controller = "Home", action = "Filter", name = String.Empty }
);
}
The idea is for me to able to navigate to http://localhost:123/home/filter/mynameparam. Here is my controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Filter(string name)
{
return this.Content(String.Format("You found me {0}", name));
}
}
When I navigate to http://localhost:123/home/filter/mynameparam the contoller method Filter is called, but the parameter name is always null.
Could someone give a pointer as to the correct way for me to build my custom route, so that it passes the name part in the url into the name parameter for Filter().
The Default route should be the last one.
Try it this way:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// My Custom Route.
routes.MapRoute(
"User_Filter",
"home/filter/{name}",
new { controller = "Home", action = "Filter", name = String.Empty }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
I believe your routes need to be the other way round?
The routes are processed in order, so if the first (default, OOTB) route matches the URL, that's the one that'll be used.