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 }
Related
I am writing a URL Shortener application.
When someone enters root.com/whatever, they are redirected to a configured URL.
I managed to create a global route which will catch the paths after the root ("whatever" above) and execute the corresponding redirection successfully.
My problem and question is this:
The admin interface is at root.com/admin and when I try to access that, I get the global controller. How do I make an exception to the global controller for "admin"?
Here is what I have in my route config right now:
routes.MapRoute(
name: "Admin",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Global",
url: "{suffix}",
defaults: new { controller = "Home", action = "Index", suffix = UrlParameter.Optional }
);
For the first route, I also tried:
routes.MapRoute(
name: "Admin",
url: "admin/{action}/{id}",
defaults: new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
And I also tried putting it second in the file.
I don't know if this will help you, but take a look at this stack overflow posting: ASP.Net MVC: Routing issue which throwing exception
This person reduced the more specific route and it worked for him.
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.
Why do we have to specify the defaults for the default route?
This is a normal default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Why can't I just do this:
routes.MapRoute(
name: "Default",
url: "Home/Index/{id}",
defaults: new {id = UrlParameter.Optional }
);
I already specified the action and controller but when I use this way, I get an error. Does anyone know why you have to specify the action and controller in the default route?
Without a default set of parameters, how is routing supposed to know where to send this URL?
/
The defaults let you do that URL, so it knows to use the 'Home' controller's 'Index' method.
Or:
/Articles
In this case, the 'Index' action of the 'Articles' controller would be called. Without those defaults, again, routing has no way to know what to do.
I have a couple of routes in my config that seem to work OK in the IIS express dev environment, but not in IIS. I can get to the route BlogArchive route, but not the BlogDetailroute.
routes.MapRoute(
name: "BlogDetail",
url: "Blog/{Slug}",
defaults: new { controller = "Blog", action = "Detail", slug = UrlParameter.Optional }
);
routes.MapRoute(
name: "BlogArchive",
url: "Blog",
defaults: new { controller = "Blog", action = "Index" }
);
This can be fixed by just adding an additional pointer to the BlogDetail i.e. making the whole url map to Blog/Entry/{Slug}, but I would rather not have to have that extra bit. I can't seem to find any examples on the web, so excuse me if I am missing something simple.
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 }
);