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

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

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.

asp.net mvc multilingual url redirect by changing url

I'm working on a multilingual Asp.Net MVC project.
Default language is Turkish and alternate language is English.
I need to make urls seo friendly. For example some urls for web site are like;
Home/About:
www.example.com/hakkinda
www.example.com/about
Home/Contact:
www.example.com/iletisim
www.example.com/contact
Home/Faq :
www.example.com/sikca-sorulan-sorular
www.example.com/frequently-asked-questions
There are some pages like this. I also have Turkish and English seo friendly urls for the pages.
I register routes:
routes.MapRoute(
name: "hakkinda",
url: "hakkinda",
defaults: new { controller = "Home", action = "About"}
);
routes.MapRoute(
name: "about",
url: "about",
defaults: new { controller = "Home", action = "About"}
);
routes.MapRoute(
name: "iletisim",
url: "iletisim",
defaults: new { controller = "Home", action = "Contact"}
);
routes.MapRoute(
name: "contact",
url: "contact",
defaults: new { controller = "Home", action = "Contact"}
);
routes.MapRoute(
name: "sss",
url: "sikca-sorulan-sorular",
defaults: new { controller = "Home", action = "Faq"}
);
routes.MapRoute(
name: "faq",
url: "frequently-asked-questions",
defaults: new { controller = "Home", action = "Faq"}
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index", id = UrlParameter.Optional }
);
I also have a Home/ChangeLanguage method, which sets thread's culture by given culture name (tr/en)
User can change language by clicking a button
My first question is i need to change url automatically when user changes language.
Initial url : www.example.com/hakkinda
After user changes language to English url should redirect to www.example.com/about
Again if user changes language to back to Turkish it should be redirected to www.example.com/hakkinda
This has to be applied to all pages.
My second question is for Seo optimization, i need to add below tags to all pages in order to say bots alternate pages by language
<link rel="alternate" hreflang="tr" href="http://www.example.com/hakkinda"/>
<link rel="alternate" hreflang="en" href="http://www.example.com/about"/>
I need to add these link tags to Site Layout and it must be matched with the current url.
Could you please give advice about how i can achieve these ?
You could create an ActionFilter that will given each request check the culture sent to the server (done via cookie?) and then have it on every request look up the controller and action against separate dictionaries, try to match the oposite localisation on either the controller or Action and if it hits a match (i.e. your currently navigating to /about but your in Turkish, it would match /about as being english and then perform a redirect to /hakkinda

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