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.
Related
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.
I have a solution, where the initial and default (RouteConfig) page is 'Login/Index'. This page, where name lets understand is used to user make login on system. After successful login, be redirected to Home page.
Login page the url showed is http://localhost:7037/ and after this, in 'Home' page is http://localhost:7037/home. How configure to controller Home in action Index dont show "home" on link? In other words, to working equal Login.
I tried some alternatives, and I still not find a solution to work in this specific case.
Thanks
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.LowercaseUrls = true;
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional },
namespaces: new [] { "Solution.UI.WEB.Controllers" }
);
}
Let's see if I understood the problemn
You have one Route "Default" only.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional },
namespaces: new [] { "Solution.UI.WEB.Controllers" }
);
What you have to do to "Home" disappear on Url is change the controler "Login" to "Home" on "defaults". It Will be like this:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new [] { "Solution.UI.WEB.Controllers" }
);
So, inside the Action "Index" on HomeController.cs you verify if the user is logged, if don't, you redirectToAction("Index","Login").
Hope that helps.
You could try via javascript replaceState on your Home View
<script>
(function () {
var path = document.URL.match(/\/\w+(?=\?)/g)[0];
window.history.replaceState('string', 'Some Title Text', path.replace('/',''));
})();
</script>
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 of asp.net-mvc4 is http://domainname.com/products/1 with routes
routes.MapRoute(
name: "Products",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
and I want to rewrite to http://domainname.com/products/1.html that has .html extention .
Any ideas for this?
Do you mean this:
routes.MapRoute(
name: "Products",
url: "{controller}/{action}/{id}.html",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
Why would you want to put .html extension to a query string or route parameter?
if you take a website with html files, the extension is with the page not with the parameters. Don't know if you have any specific requirement to put it always at the end of the url. but it doesn't make any sense.
routes.MapRoute(
name: "Products",
url: "{controller}/{action}.html/{id}",
defaults: new { controller = "Products", action = "Index", id = UrlParameter.Optional }
);
you might have problems with iis after doing the above, please take a look at this discussion as well.
ASP.NET MVC Routing - add .html extension to routes
I simply wants to route a URL which is not in the MVC project.
Like
http://mysite.com/Parents/default.aspx?ID=xxx
TO
http://mysite.com/accounts/login
with the ID
I think something like this would work.
routes.MapRoute(
name: "Default",
url: "Parents/default.aspx?ID={id}",
defaults: new { controller = "Accounts", action = "Login", id = UrlParameter.Optional }
);
If you want to redirect all .aspx pages to redirect at "accounts/login" then do like below
routes.MapRoute(
"Page",
"{name}.aspx",
new { controller = "Accounts", action = "Login", id = UrlParameter.Optional }
);