Disabling Default Routes on Asp.net Mvc - c#

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.

Related

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.

Config routing asp.net mvc

if i have routing:
routes.MapRoute(
name: "Default",
//url: "{action}",
url: "{controller}/{contynent}/{country}",
defaults: new { controller = "Home", action = "Index", kontynent=UrlParameter.Optional , panstwo= UrlParameter.Optional }
);
now in my page i can use:
localhost:4084/Home
localhost:4084/World
localhost:4084/World/Europe
localhost:4084/World/Europe/England
but in this case i can use only action equals Index and #Html.ActionLink don't give any results :( (moves me to: http://localhost:4084/?action=Action&controller=Controller).
How change MapRoute to form, where i can use:
localhost:4084/Home/About (controller=Home, action=About)
localhost:4084/Home/SignIn (controller=Home, action=SignIn)
localhost:4084/World/Europe (controller=World, action=Index, contynent=Europe)
localhost:4084/World/Europe/England (controller=World, action=Index, contynent=Europe, country=England)
Best way i want to create exception for World controller in MapRoute.

Rewriting url in asp.net-mvc4 (razor) with extention

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

root directory of MVC not found

I'm new to MVC4 (or MVC at all using .NET). I have my controller and views working fine but if I navigate to
http://localhost:<port>
I get a 404. If I go to "
http://localhost:<port>/MyController
everything works fine. How do I get a default controller for ROOT of the website?
Your default route is handled by "RouteConfig.cs" which is in the folder "App_Start"
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
In fact, I never use this RouteConfig anymore. I prefer using attributeRouting. It allows to define the routes via an attribute above your controller. http://attributerouting.net/ or more precisely http://attributerouting.net/#defining-routes
It depends on what your 'root controller' is called and what the name of your landing action is. if it is MyController.Index() then you would do
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "My", action = "Index", id = UrlParameter.Optional }

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

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 }
);

Categories

Resources