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?
Related
routes.MapRoute(
name: "PNM",
url: "PNM/{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "DTE",
url: "DTE/{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "CE",
url: "CE/{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
I have routes configured like this. When I run the web site I get this url localhost:12345/
and then I can type any client name to get the client specific logo
localhost:12345/CE which takes me to the login page with CE logo.
Here's the issue : Once I login URL gets changed to http://localhost:12345/PNM/Home/Index
while I am in CE url should be http://localhost:12345/CE/Home/Index
How should I write my routeconfig file? or what should I change that it goes to specific-client url all the time when I navigate to any page in the website?
Why don't you use a single route and move the client name as a parameter?
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{clientName}/{id}",
defaults: new { controller = "Account", action = "Login", clientName = UrlParameter.Optional, id = UrlParameter.Optional }
);
This approach is more dynamic than specifying different routes for each client. I haven't tested this yet, just trying to give you a different approach.
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.
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 }
);
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" });
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 }
);