Asp MVC 4 MapRoute specific url - asp.net-mvc

I have web site root structure controllers for work with culture part in url,
like mysite.com/en/controller/action/id
but for one specific controller I don't want to use culture prefix.
mysite.com/controller/action/id
How I can do it ?
I have write it MyRoute but it dos't work
routes.MapRoute(
name: "MyNewRoute",
url: "MyNewRoute/",
defaults: new { controller = "MyNewRoute", action = "Data", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "MemberTransfer",
url: "en/Member/Transfer",
defaults: new { culture = "en", controller = "Member", action = "Transfer", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "ExecuteTransfer",
url: "en/Member/ExecuteTransfer",
defaults: new { culture = "en", controller = "Member", action = "ExecuteTransfer", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "",
defaults: new { culture = "en", controller = "Home", action = "Maintenance", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "HomePage",
url: "{culture}/{*pathInfo}",
defaults: new { culture = "en", controller = "Home", action = "Maintenance", id = UrlParameter.Optional }
);

Your "Default" route is overriding the last one. Try to swap the last two routes.
Btw, you can use handy route debugger from Phil Haack. It's really easy to set up and use (you just need to comment/uncomment one single line in your Global.asax.cs). It's absolutely must have tool for every Asp.Net MVC developer.

Swap your last two routes. Routes are evaluated top down, going with the first match, so you want to go from most specific to most generic in the order that you add them.

Related

Custom route breaks Default routing Asp.Net

I have custom routing for controller added right over the default one:
Custom:
routes.MapRoute(
name: "FaqSubCategory",
url: "{culture}/{controller}/{action}/{TapCode}",
defaults: new { controller = "FAQ", action = "GetChosenFaqSubCategory", TapCode = UrlParameter.Optional },
constraints: new { culture = new CultureConstraint() }
);
Custom v2:
routes.MapRoute(
name: "FaqSubCategory",
url: "{culture}/{controller}/{action}/{TapCode}",
defaults: new { controller = "FAQ", action = "GetChosenFaqSubCategory", TapCode = UrlParameter.Optional },
constraints: new { culture = new CultureConstraint(), FAQ = new TranslateControllersConstraint("4189") }
);
Default:
routes.MapRoute(
name: "DefaultWithCulture",
url: "{culture}/{controller}/{action}/{ID}",
defaults: new { controller = "Home", action = "Index", ID = UrlParameter.Optional },
constraints: new { culture = new CultureConstraint() }
);
Basically because of that custom route, home page is receiving "/home/index" and some of the other pages are receiving "/index" at the end of the URL
When I write the custom routing like the second variant, then everything is working perfectly except the FAQ controller.
When an MVC application first starts it creates the route table.The default route table contains a single route named Default. So default route should have Default name, instead of DefaultWithCulture an your default route should have default culture
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{ID}",
defaults: new { culture="en", controller = "Home", action = "Index", ID = UrlParameter.Optional },
.....

mvc routes not working

I have two routes in RouteConfig
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Desktop", action = "Index", wizard = "", culture = "he-IL", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Wizard",
url: "{wizard}/{culture}/{controller}/{action}/{id}",
defaults: new { controller = "Desktop", action = "Index", wizard = "AllManufucturer", culture = "he-IL", id = UrlParameter.Optional }
);
I'm trying to navigate to
http://localhost:5754/he-IL/Account/Login which is working
http://localhst:5754/wizard234/he-IL/InfoRequest/Details which is not working
I tried to change the routes order and link 1 is not working and link 2 is working, I want the site will run two different routes template
Please what am I missing?
Tank you in advanced

Does MVC routes look for pattern or names

I am trying to understand routes, but am very confused.
The question is simply, does the default values have any impact on which router is chosen, or is it simply the pattern.
For example, consider the following
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{startIndex}",
defaults: new { controller = "Home", action = "Index", startIndex = UrlParameter.Optional }
);
routes.MapRoute(
name: "About",
url: "{controller}/{action}/{startIndex}",
defaults: new { controller = "About", action = "Index", startIndex = UrlParameter.Optional }
);
Regardless of whether the end result is the same, would MVC simply choose the first every time since the URL: pattern matches the request, and therefore ignore that they have different controllers?
Route selection uses pattern matching and will select the first matching pattern. In your example, there's no reason for your second route as the first will match /about/... as well as the second and result in the same action being invoked. If you need to have routes which have the same basic pattern, perhaps the values in the pattern affect the controller you use, you can use routing constraints to aid in choosing the correct route or use fixed values and place the route before the default route.
routes.MapRoute(
name: "Contact",
url: "/contact",
defaults: new { controller = "about", action = "contactus", id = "" }
);
routes.MapRoute(
name: "Help",
url: "/help",
defaults: new { controller = "about", action = "help", id = "" }
);
routes.MapRoute(
name: "Admin",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Admin", action = "Index", id= UrlParameter.Optional },
constraints: new { controller = "(admin)|(orgadmin)" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{startIndex}",
defaults: new { controller = "Home", action = "Index", startIndex = UrlParameter.Optional }
);

Url without controller and action

I'm making asp.net mvc application and I have next issue. For example I need produce url like this www.something.com/abc where abc is product ID and www.something.com/def where def is company ID.
Can someone show me some part of code with route link like this?
#Html.RouteLink("Sample link 1", "routeName 1",
new {controller = "Home", action = "action name 1", parameter="abc" })
#Html.RouteLink("Sample link 2", "routeName 2",
new {controller = "Home", action = "action name 2", parameter="def" })
Just to clarify more my question, for example:
this is routing system
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "aaaaa",
url: "{id}",
defaults: new { controller = "Home2", action = "Index2" }
);
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "bbbb",
url: "{id}",
defaults: new { controller = "Home3", action = "Index2" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
And these are routelinks
#Html.RouteLink("bbbb", "aaaaa",new { id = 555 })
#Html.RouteLink("bbbb", "bbbb", new { id = 6666666, controller="Home3"})
And both of them are redirecting me to same action controller home2 and action Index2.
But I specified which route to use "aaaaa" for first and "bbbb" for secound
And I also specified different controller in second one.
You cannot have 2 identically looking urls:
http://example.com/555
http://example.com/6666666
be routed to 2 different controller actions. The routing engine has absolutely no way of disambiguating between them. When a request of this form comes in, the routing engine evaluates your routes in the order they are defined and it matches this one:
routes.MapRoute(
name: "aaaaa",
url: "{id}",
defaults: new { controller = "Home2", action = "Index2" }
);
That's the reason why Home2 controller is executed. You should distinguish between the notion of generating an url (with the Html.RouteLink helper) where you have the possibility of specifying the route name and evaluating a route.
If you want to be able to disambiguate between those 2 urls you will need to use constraints. For example:
routes.MapRoute(
name: "aaaaa",
url: "{id}",
defaults: new { controller = "Home2", action = "Index2" },
constraints: new { id = #"\d{1,3}" }
);
routes.MapRoute(
name: "bbbb",
url: "{id}",
defaults: new { controller = "Home3", action = "Index2" },
constraints: new { id = #"\d{4,10}" }
);
In this example the first route accepts ids with 1 to 3 digits whereas the second route accepts ids with 4 to 10 digits.

asp.net mvc route with action after id and slug

I'm adding a new route into my mvc web application and it's not working as desired. I was hoping someone could help me figure out where in the list it should go and maybe which defaults and/or constraints should be defined for it (in RouteConfig.cs).
The desired route would look like so:
/controller/id/slug/action e.g. mydomain.com/products/10/product-name/reviews
I've tried to define this route like so and have tried it as the 1st, 2nd and 3rd routes listed:
routes.MapRoute(
name: "AlternateRoute",
url: "{controller}/{id}/{slug}/{action}",
defaults: null,
constraints: new { id = #"\d+", slug = #"[\w\-\d+]*" }
);
What's happening is after I add the above route, and browse to a page like /products/10/product-name - url's that were previously something like /products/create look like /products/10/product-name/create (but only on that page).
The only other routes I have are these 3 (defined in my routeConfig file):
/controller/id/slug
routes.MapRoute(
name: "DefaultSlugRoute",
url: "{controller}/{id}/{slug}",
defaults: new { action = "Details", slug = "" },
constraints: new { id = #"\d+", slug = #"[\w\-\d+]*" }
);
/controller/action/year/month
routes.MapRoute(
name: "MonthlyArchiveRoute",
url: "{controller}/{action}/{year}/{month}",
defaults: new { controller = "Blog", action = "Archives", year = UrlParameter.Optional, month = UrlParameter.Optional },
constraints: new { year = #"\d{4}", month = #"\d{2}" }
);
/controller/action/id (the standard one included w/ a new mvc project)
routes.MapRoute(
name: "DefaultRoute",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Resources