i am searching the net non-stop.
Can pls someonw point me in the rigth direction?
My Problem:
Normally mvc display: {Controller}/{Action}
and it gives us => Home/Index
What i want:
I want different URL without changing action or controller name like =>
MyPage/MyIndex
Should i be looking into the routing or is there an other way.
try this:
routes.MapRoute(
name: "CustomRoute",
url: "MyPage/MyAbout",
defaults: new { controller = "Home", action = "About"}
);
Related
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
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.
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
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
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.