How to get URL of Default Action in ASP.NET MVC - 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

Related

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.

asp.net mvc routing routelink creating route defined first in application.cs

I am new to MVC and little confused with routing. I have defined 2 routes as
 
routes.MapRoute(
"testingController1",
" /mobile/{controller}/{action}/{key}",
defaults: new { controller = "Controller1", action = "Home" });
routes.MapRoute(
"testingController2",
"/desktop/{controller}/{action}/{key}",
defaults: new { controller = " Controller2", action = "Home" });
 
 
As seen above I have 2 routes. Now on using a route link for “testingController2” with Controller=” Controller2”, Action =”Products” the url is redirected rendered in html is /Mobile/Controller2/Products. How to resolve the above issue. I want it to be rendered as /desktop not Mobile.
The way you have implemented, to test you will have to call the url like:
/desktop/testingController2/Products/
Another way to do what you did could be using the concept of Areas. Please take a look at this link and take a look at the steps 13 and 14 on how to register areas.
Unless there is a special requirement to do what you did, you could just leave the default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index", id = UrlParameter.Optional }
);
And then create a controller called MobileController and another called desktopController.
By then adding Actions called Products you would be able to use the links such as:
www.yoursite.com/mobile/products
www.yoursite.com/desktop/products

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 }
);
}

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.

Can I get an ASP.MVC route from a url?

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.

Resources