Can I get an ASP.MVC route from a url? - asp.net-mvc

I define routes in my global.asax, like this:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
Given a url:
/somecontroller/someaction/3
is it possible to determine which route this will map to, and get the corresponding route object, with name, url template and defaults?
Thanks

Phil Haack has a blog post with an ASP.NET Routing Debugger that will let you debug any and all routes.

Related

How to get URL of Default Action in ASP.NET MVC

I want to get URL of the default controller's default action method link, such that if I change default controller and action in route config then should be updated in the view too.
e.g.
Url.Action(defaultAction,DefaultController);
//output should be like
Url.Action("Index","Home")
Assuming the default route is named Default (in your RoutesConfig):
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
You can use Url.RouteUrl to get the url:
Url.RouteUrl("Default")
See MSDN

how to add multiple Url in MVC5 route.config

I am new to ASP.NET MVC5, I found one default route in routeconfig.cs file.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
so, If I am trying to access localhost:44300, I am redirecting to Home/Index action method.
So I am added Login page in Home/Index view page. after user successful login I am redirecting the user to Home/Details Method then In browser I am getting url as https://localhost:44300/Home/Details.
Here my question Is there any possibility to hide Home/Details like Home/Index. I am tried with adding another default route but it is failed.
Try adding following route before your default route in RouteConfig.cs file -
routes.MapRoute(name: "Details",
url: "Home/Index/{id}",
defaults: new { controller = "Home", action = "Details", id = UrlParameter.Optional }
);
This will convert - Home/Details to Home/Index
It is impossible to have the same url (http://localhost:44300) be mapped to 2 different controller actions. There can only be one default route.

How to prevent showing "Index" in mvc url

I use ActionLink in my MVC4 website as follows:
#Html.ActionLink("home", result: MVC.Home.Index())
after redirect, the url comes in the "www.mysite.com/home/index" form.
How can I prevent showing "home/index" or "index" in url??
You are seeing index in url because html.actionlink generates tag with href home/index. Instead of using helper html.actionlink use tag directly with href as home and have a default route which routes to index. But in this case you might loose the ability to route correctly when deployed in virtual directory.
You have to supply defaults to the corresponding route in your route configuration like so:
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 }
);
}

ActionLinks reverting to querystring params when matching a route that does not contain that param

I'm trying to achieve this goal of changing my apps routes to look like this:
hxxp://host/MyController/Widgets/3/AddWhatsit
The view for this route would help a user add a Whatsit to Widget 3.
Similarly, I would expect the route to create a new Widget to be:
hxxp://host/MyController/Widgets/Create
I've created separate routes to try and facilitate this. They are:
routes.MapRoute("DefaultAction",
"{controller}/{action}",
new {controller = "Home", action = "Index"});
routes.MapRoute("Default",
"{controller}/{id}/{action}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional});
The problem I'm having is that when I browse to the Index page for Widgets (/MyController/Widgets, matching the "DefaultAction" route) Any ActionLinks that would introduce a new url parameter that's not part of that route gets turned into a querystring value. So, for example, the edit link for Widget 3 would render as:
Widget/Edit?id=3
instead of (what I would prefer):
Widget/3/Edit
I guess I understand that I'm messing things up by not putting my (optional) id param at the end of the route.
Should I suck it up and just leave id at the end of the route?
It is possible to achieve this. To get anchor link looking like /Home/1/Index, set routes like:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Custom",
url: "{controller}/{id}/{action}"
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
And then, in the View:
#Html.ActionLink("Here", "Index", "Home", new { id = 5 }, null)
And you get link rendered like this:
Here
Quirk is to constrain custom route. I removed defaults in this case, they do not make sense. And order of routes, of course.
I believe you need to change the order of your routes. Remember, that MVC looks down the route list and picks the First matching route. Your second route with the ID parameter is more specific and as such, should come first in your routing table.
Even though you have specified an ID parameter in your ActionLink, you have also specified a controller and an action. Therefore, the first route is being chosen by the RoutingEngine.
Lastly, remove the optional parameter for the ID attribute. Since you want that route to be chosen when you have an Id, then you do not want that to be an optional parameter, you want it to be required to match that route.
routes.MapRoute("Default","{controller}/{id}/{action}",
new {controller = "Home", action = "Index"});
routes.MapRoute("DefaultAction", "{controller}/{action}",
new {controller = "Home", action = "Index"});

How to Route /About to /Home/About

I am just getting started with ASP.NET MVC and it's great! However, I don't quite understand setting up routes.
How do I route ~/About to ~/Home/About?
/Views/Home/About.aspx
I would like to be able to access it with
/Home/About
or just
/About
If you want to explicity setup a route for it, you can do something like this:
routes.MapRoute(
"AboutRoute",
"About",
new { controller = "Home", action = "About" } // Parameter defaults
);
I think thats what you want to do? I.e. have /About handled by the home controller?
The default route (as below) handles /Home/About
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
In response to your comment on RM's answer - you don't actually need wildcards for that. Just do
routes.MapRoute(
"AllToHomeController",
"{action}/{id}",
new { controller = "Home", action = "Index", id = "" });
Note, however, that you need to place this route at the very end of your route table (and you'll have to delete the default route), as this will catch every url that comes in.
You can use Phil Haack's Route Debugger to verify that your routes pick up urls as you expect.

Resources