How to set multiple routes to same action method in MVC 5? - asp.net-mvc

I am working on single page application using MVC 5 and Angular 2.
My requirment is I need do set multiple routes to same controller/action method.
If user enters url http://localhost:xxxx/home/index , it will target "index" action method of "home" controller.
I also
want "http://localhost:xxxx/products" and "http://localhost:xxxx/categories" to point to "index" action method of "home" controller.
Note : Both "products" and "categories" are netiher controller or action methods.

Adding below 2 routes did the job.
routes.MapRoute(
name: "categories",
url: "categories",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "products",
url: "products",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Related

Routes in ASP.NET MVC 5

My requirement is I need to set multiple routes to same controller/action method.
If user enters url http://localhost:xxxx/home/index , it will target "index" action method of "home" controller.
I also want "http://localhost:xxxx/products" and "http://localhost:xxxx/categories" to point to "index" action method of "home" controller.
I was able to achive this by adding two routes "categories" and "products" as mentioned below , and it is working fine.
routes.MapRoute(
name: "categories",
url: "categories",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "products",
url: "products",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
My question is , is there any way I combine those two routes "categories" and "products" in to one ?
You can achieve this by adding a constraint to your route.
Make the entire path a parameter, and then assign a regular expression rule to match this parameter.
routes.MapRoute(
name: "IndexMapper",
url: "{alternateIndexName}",
defaults: new { controller="Home", action="Index" },
constraints: new { alternateIndexName="(categories)|(products)"}
);
https://msdn.microsoft.com/en-us/library/cc668201.aspx#Anchor_6

MVC Routes for an multi tenant application

I have a MVC application and can't quite get the routing working for my multi-tenant application. Here is the problem:
I have 2 types of pages in my application, most require the tenant name to be in the url but some don't. e.g.
These Do (tenant name is these examples is samsung and apple):
http://www.mytestapp.com/samsung/customers/add
http://www.mytestapp.com/apple/customers/add
These Don't:
http://www.mytestapp.com/home/register/
http://www.mytestapp.com/home/aboutus/
What routes do i require to get this working? I have tried this but it does not work for the register and about us page.
routes.MapRoute(
name: "TenantRoute",
url: "{tenantid}/{controller}/{action}/{id}",
defaults: new { tenantid = "tenantname", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Well, you'll need a second route to match the non-tenant routes. The default one should match if register and aboutus are controllers with an Index action:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Register", action = "Index", id = UrlParameter.Optional }
);
try with this
routes.MapRoute(
"samsung",
"samsung/{controller}/{action}/{id}",
new { controller = "YourController", action = "YourAction", id = UrlParameter.Optional}
);
routes.MapRoute(
"apple",
"apple/{controller}/{action}/{id}",
new { controller = "YourController", action = "YourAction", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Register", action = "Index", id = UrlParameter.Optional }
);

Asp.net mvc routing without a controller or action name

I am trying to redirect all urls that don't match an existing controller to a certain controller.
For example, the url mywebsite.com/newyork should be processed as mywebsite.com/Cities/Info/newyork
I am using the following code in my RegisterRoutes but it doesn't seem to work as I get a 404 reponse:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Cities",
url: "{cityname}",
defaults: new { controller = "Cities", action = "Info", cityname= "" }
);
You should put your cities route first and drop the empty default parameter:
routes.MapRoute(
name: "Cities",
url: "{cityname}",
defaults: new { controller = "Cities", action = "Info" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The routes are processed in order so you should have most specific first to least specific ( your default route).
As your website.com/newyork matched the default route, it wasn't continuing to your city route.

How can I make my routes that start with /home always direct to the home controller and index action?

I have the following route:
routes.MapRoute(
name: "Default",
url: "home/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This works fine and directs to the home controller Index action where the user enters /home
Is there a way that I can ALWAYS make it direct to the Index action if the user enters:
/home/xxx or
/home/xxx/xxx
Where xxx can be anything?
Sure, just remove the {action} part from your route definition so that the action is always Index:
routes.MapRoute(
name: "Default",
url: "home/{anything}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
If you remove the {action} from the url and keep it as a default value then it should work fine.
routes.MapRoute(
name: "Default",
url: "home/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
In this scenario action will ALWAYS be Index...
Try this to catch every possible route:
routes.MapRoute(
"Default",
"home/{*url}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional } }
);

How define this routes in asp.net mvc4?

I am beginner in asp.net mvc.
I have 2 Controllers:
HomeController actions: index,about
url that i need:
index action url: mydomain
about action url: mydomain/about
OtherController actions: index
index action url: mydomain/other
MyCode That not works:
routes.MapRoute(
"Other",
"{controller}/{action}/{id}",
new { controller = "Other", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Thanks
For your Home Controller, to hit
index action url: mydomain
you need
routes.MapRoute(
name: "Home",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
to hit
about action url: mydomain/about
you need
routes.MapRoute(
name: "Home",
url: "about",
defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
);
For your Other Controller, to hit
index action url: mydomain/other
you need
routes.MapRoute(
name: "Other",
url: "other",
defaults: new { controller = "Other", action = "Index", id = UrlParameter.Optional }
);
Note that, in all cases, the "name" parameter doesn't really matter.
I think you missed {controller} in your Default routing spec.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Resources