Route base/id to controller/action/id in mvc 4 - c#

I want to redirect users when they enter a URL into the browser based on the ID. For example, a user enters:
http://localhost:50431/10213
and they will be redirected to:
http://localhost:50431/home/job/10213
Default Route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{jobno}",
defaults: new { controller = "Home", action = "Job", jobno = UrlParameter.Optional }
);
How can I achieve this?

You could try something like this:
routes.MapRoute(
name: "Job Number",
url: "{jobno}",
defaults: new { controller = "Home", action = "Job" },
new { jobno = #"[0-9]*" }
);
And put it above the other route. The added parameter is to avoid the route catching urls like http://localhost:50431/foobar, but only the ones that contain numbers.
Please note I don't have a way of testing this at the moment so you may have to tweak it slightly.

I think this is the best solution:
routes.MapRoute(
name: "Job Number",
url: "{jobno}",
defaults: new { controller = "Home", action = "Job", .jobno = UrlParameter.Optional },
constraints: new { jobno = #"[0-9]*" }
);
I have tested this and it works properly, his reply in the comment did not work properly for me.

Related

MVC - RouteTable Redirect to another route

I've been developing a product for 2 years which is being sold by ourselves and partners.
Now the manager wants to give the source code to our partners which will be only used as Demo apps with a Demo License.
All the demo module has been already developed but I need to show "Demo" on the route (url) when the license says so.
Example:
Every call to
/Controller1/Action1
Must transform the URL to
/Demo/Controller1/Action1
First try:
routes.MapRoute(
name: "DefaultDemo",
url: "Demo/{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 }
);
However, this will partially work because I need to set "Demo" on every call to Controller/Action I've in my app.
Second try:
routes.MapRoute(
name: "DefaultDemo",
url: "{demo}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { demo = "Demo" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, demo = "Demo" }
);
In this one, I tried to set it as a variable and "Demo" as a constraint of the that variable, to prevent someone to change it to other text.
Then I tried to use the "Default" route as Redirect to the other one, by setting the parameter "demo", however this obviously failed since it doesn't work this way.
Third try:
var demoRoute = routes.MapRoute(
name: "DefaultDemo",
url: "{demo}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { demo = "Demo" }
);
routes.Redirect(x =>
x.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional, demo = "Demo" })
)
.To(demoRoute);
In the last example I tried to use RouteMagic nugget to redirect the old "Default" route to the Demo one, but it didn't work.
Anyone who did something similar?

asp.net mvc routing - map actions not controllers

I know it's more of the same (SO has more than 5,600 questions on this), but I've been sitting on this one for a couple of days now so I guess it was the right time to ask a question.
My requirements
I want to have the following routes in my asp.net mvc app:
myapp.com/sigin -> Controller = Account, Action = SignIn
myapp.com/signout -> Controller = Account, Action = SignOut
myapp.com/joe.smith -> Controller = User, Action = Index
myapp.com/joe.smith/following -> Controller = User, Action = Following
myapp.com/joe.smith/tracks -> Controller = Tracks, Action = Index
myapp.com/about -> Controller = About, Action = Index
Any other default route, so that's why I left the standard one there.
My Code
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "About",
url: "about",
defaults: new { controller = "About", action = "Index" }
);
routes.MapRoute(
name: "MyTracks",
url: "{username}/tracks",
defaults: new { controller = "MyTracks", action = "Index" }
);
routes.MapRoute(
name: "MyTunes",
url: "{username}/tunes",
defaults: new { controller = "MyTunes", action = "Index" }
);
routes.MapRoute(
name: "MyProfile",
url: "{username}",
defaults: new { controller = "User", action = "Index"},
constraints: new { username = "" }
);
routes.MapRoute(
name: "Account",
url: "{action}",
defaults: new { controller = "Account" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Issue
Routes number 3 and 4 just don't work as they get mixed up with route 1 and 2. I have tried debugging my code with Phil Haack's routing debugger, but no luck. Any ideas what I am doing wrong?
The problem is in your last two custom routes. All the routing framework has to work with is just a single token from the URL, and two possible routes to match it with. For example, if you attempt to go to the URL, /signin, how is the routing framework supposed to know there's not a user with username "signin". It's obvious to you, a human, what should happen, but a machine can only do so much.
You need to differentiate the routes in some way. For example, you could do u/{username}. That would be enough to help the routing framework out. Short of that, you'll need to define custom routes for each account action before the user route. For example:
routes.MapRoute(
name: "AccountSignin",
url: "signin",
defaults: new { controller = "Account", action = "Signin" }
);
// etc.
routes.MapRoute(
name: "MyProfile",
url: "{username}",
defaults: new { controller = "User", action = "Index"},
constraints: new { username = "" }
);
That way, any of the actual action names will match first.

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.

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