Add routing in case of missing "action" in url ({controller}/{id}) - c#

How can I set up a {controller}/{id} routing in ASP.NET MVC 5.
What I would like to achieve: if there are no {action} defined, call Index() with id.
I tried thism but didn't work:
// Keep default routing
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// Add own routing in case of missing "action"
routes.MapRoute(
name: "Controller/Id",
url: "{controller}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

After reading ameer's comment it become clear that the routing is not that "intelligent" that I was hoping for, so if the URL pattern is matching one route, but no such controller/method found, it will not "fall through" to the next routing command, but will raise exception.
So I had tried what Simon suggested, added constraint to my custom routing, and reversed the order, and it works now. However, if I would like to have other mappings similar to attachments, I'd have to add them one by one.
Working code:
routes.MapRoute(
name: "Attachments",
url: "attachments/{id}",
defaults: new { controller = "Attachments", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Related

Disabling Default Routes on Asp.net Mvc

I am using Asp.net Mvc 5 with C#.
I want to disable default routing in my project. My map routes like;
routes.MapRoute(name: "News",
url: "haberler",
defaults: new { controller = "News", action = "Index"});
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
When a user visits my website's news page, it's like;
www.domain.com/haberler
But also the user can visit the news page as below;
www.domain.com/news
I want to remove that "/news" or direct to seo-friendly url like; "/haberler"
So how can I disable default routing (Controller-Name-Convension) routing?
Instead of removing the "default" route, you can add a controller constraint to it
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { controller = #"(Account|Manage|Home)" }
);
Like this, /news will return 404 Not Found.
In MVC, the convention is to map URLs to a particular Action on a particular Controller. Remove default route to disable default convention:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
www.domain.com/news will not match in any of your routes hence it will throw an error.
Try removing with this
RouteTable.Routes.Remove(RouteTable.Routes["NAME ROUTE YOU WISH TO RMOVE"]);
Hope this will helps you.

MVC Routes interference issues

I want to have the following:
Link to {controller}/{destination} and link to {controller}/{action}, for example: flights/berlin and flights/search accordingly.
My routes config is as follows:
routes.MapRoute(
name: "LandPage",
url: "{controller}/{destination}",
defaults: new { controller = "Flights", action = "Index", destination = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
If "LandPage" is first, the route will go always to the land page with the url parameter (i.e. --> flights/search will go to flights/index with parameter destination = search) and its bad for me.
If "Default" will be first, and I try to navigate to flights/berlin, it will try to navigate to the flights controller and action = berlin, of course no such action...
The only solution I can think of is using "LandPage" first, and compare the {destination} parameter with name of action and redirect to that action... I don't like that solution... anyone can think about another solution??
Thanks!
You can set fixed routs for specific actions:
routes.MapRoute(
name: "Search",
url: "Flights/Search/{search}",
defaults: new { controller = "Flights", action = "Search", search = UrlParameter.Optional }
);
and
routes.MapRoute(
name: "LandPage",
url: "Flights/{destination}",
defaults: new { controller = "Flights", action = "Index", destination = UrlParameter.Optional }
);
before your default route.

How can I route to localhost:2233/username?

In mvc 4 I am trying to route to something like this http://localhost:22128/username. In my route.config
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
For this do I need to change url format? how can I route ? Can someone help me to solve this? I am very confused on this.
Edit
I tried this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"UserProfile",
"{username}",
new { controller = "Profile", action = "Index", username = string.Empty }
);
but Still it not finding the profile controller in index method.
Routes are order specific. They are tried from the first route registered to the last.
Since the default route matches every URL with 0, 1, 2, or 3 segments no URL defined after it with any of those lengths will work.
So you need to define your UserProfile route before the Default route.
routes.MapRoute(
"UserProfile",
"{username}",
new { controller = "Profile", action = "Index", username = string.Empty }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

default routing not working

In my MVC application, I have following route configuration,
routes.MapRoute(
name: "ProductRoute",
url: "{productName}/{controller}/{action}/{id}",
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 }
);
Now if i give some thing like this, localhost:56789/prd1/Home/Index
The first routing is working.
However if i directly access localhost:56789/Home/Index or any other controller action, like localhost:56789/Account/Login the routing is not working.
Is it because I have a {productName} defined?
For testing purpose how can I add few products like prd1, prd2 along with routing?
Routes are matched against the request in the order they are defined. When a route matching the request is found, no further routes are considered. Thus, you need to list your routes in order of decreasing specificity.
However, your first route matches more requests than your second route, i.e. it is less specific than the second route:
When ASP.NET MVC tries to match the request for Home/Index to your routes, it will match the first route because it will consider Home to be the productName, it will consider Index to be the controller name and the rest of the parameters are not required.
You need to reorder your routes or make the first route more specific. This could be done by putting constraints on the productName parameter.
UPDATE
Without knowing anything about your products and their names, it is impossible for me to suggest an appropriate constraint. Maybe you could use a numeric SKU and have a constraint like
routes.MapRoute(
name: "ProductRoute",
url: "{productName}/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { productName = #"\d+" }
);
forcing productName to be numeric.
Alternatively, you could change the url to
"products/{productName}/{controller}/{action}/{id}"
For more on constraints see this link or use Google.
For route configuration
routes.MapRoute(
name: "ProductRoute",
url: "{productName}/{controller}/{action}/{id}",
defaults: new { productName = 'put your method name to get productName over here', controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "your namespace of controller" });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });

OR operator in MVC URL pattern

I'm writing an MVC route and i was wondering, can i use something like an OR operator in it?
Something like this, where my Urls can either start with "shop/" or with "articles/"?
routes.MapRoute(
name: "Default",
url: "shop|articles/{action}/{id}",
defaults: new { controller = "Store", action = "Index", id = UrlParameter.Optional }
);
You could use a route constraint where you specify the valid values separated by pipes.
routes.MapRoute(
name: "Default",
url: "{page}/{action}/{id}",
defaults: new { controller = "Store", action = "Index", id = UrlParameter.Optional },
constraints: new { page = "shop|articles" });

Categories

Resources