There are regular pages on the site such as:
foo.com/about
foo.com/contact
I use single controller for these.
There is also pages for admin, i have a seperate controller:
foo.com/admin/createsomething
This is my routing config and it is not picking HomeController Actions.
routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Admin",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
How can i fix it?
You need routes for the urls that break the default convention:
routes.MapRoute(
name: "About",
url: "about",
defaults: new { controller = "Home", action = "About" }
);
routes.MapRoute(
name: "Contact",
url: "contact",
defaults: new { controller = "Home", action = "Contact" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
then your admin url should resolve with the default route, where you have an AdminController and a CreateSomething action method
Related
I have a "HomeController" and my route is like this
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home", action ="Index", id = UrlParameter.Optional
}
);
I want url be like "web-design/Index" instead of "Home/Index".
how can i do that ?
thanks
I would do it like this:
// add a new route
routes.MapRoute(
name: "homepage",
url: "web-design/{action}",
defaults: new {
controller = "Home", action ="Index"
}
);
// add your default route but change the default action or controller
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "Home", action ="SomeotherAction", id = UrlParameter.Optional
}
);
If you want "web-design/Index" to be the default route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new {
controller = "web-design", action ="Index", id = UrlParameter.Optional
}
);
I have only default route enabled:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Which will resove paths like: hostname/Home/Index, hostname/Home/Foo, hostname/Home/Bar/23 just fine.
But I must also enable route like this: hostname/{id} which should point
to:
Controller: Home
Action: Index
{id}: Index action parameter "id"
Is such a route even possible?
If id is a number you could add a route above the other one and define a regex constraint:
routes.MapRoute(
name: "IdOnlyRoute",
url: "{id}",
defaults: new { controller = "Home", action = "Index" },
constraints: new { id = "^[0-9]+$" }
);
Not Tested :
Create your route like this
routes.MapRoute(
name: "custom",
url: "{controller}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
under your default route
Since I'm short on time, I have configured route for every action. Luckly there are not many of them. :)
routes.MapRoute(
name: "IndexWithParam",
url: "{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "IndexWithOutParam",
url: "",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Login",
url: "Home/Login",
defaults: new { controller = "Home", action = "Login" }
);
So last route was repeated for ever other action. Not sure if it can be done better, but it works.
Have you tried to play around with AttributeRouting package?
Then your code will look like this
[GET("/{id}", IsAbsoluteUrl = true)]
public ActionResult Index(string id) { /* ... */ }
Defined a route:
routes.MapRoute(
name: "SearchRoute",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Search",
action = "Index", id = UrlParameter.Optional }
Index method:
public ActionResult Index(int? id, SearchViewModel model)
I browse to http://xxx.xxx.xxx.xxx/Search and my Index action is
hit on my SearchController, good.
I browse to http://xxx.xxx.xxx.xxx/Search/1 and I get a 404 error.
I browse to http://xxx.xxx.xxx.xxx/Search/Index/1, OK.
I'd rather work with 2 above and not have to type in /Index/1. How?
You can create your route like this:
routes.MapRoute(
name: "SearchRoute",
url: "{controller}/{id}",
defaults: new { controller = "Search", action = "Index", id = UrlParameter.Optional }
);
It will only ever target the Index action but it sounds like what you want.
There is priority in routing, ie: which route you define first matters. Try following:
routes.MapRoute(
name: "SearchRoute",
url: "Search/{id}",
defaults: new { controller = "Search",
action = "Index", id = UrlParameter.Optional }
routes.MapRoute(
name: "DefaultRoute",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index", id = UrlParameter.Optional }
I have the following routes:
routes.MapRoute(
name: "One",
url: "admin/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Two",
url: "home/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Is there a way that I can combine these routes into just one route where the they both direct to the "Home" controller?
Try
routes.MapRoute(
name: "Default2",
url: "{section}/{id}",
defaults: new {section="home" ,controller = "Home", action = "Index", id = UrlParameter.Optional }
);
works for me
answer on comment.
what a problem add the constraint?
routes.MapRoute(
name: "Default2",
url: "{section}/{id}",
defaults: new { section = "home", controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { section = "admin|home" }
);
I am beginner in asp.net mvc.
I have 2 Controllers:
HomeController actions: index,about
url that i need:
index action url: mydomain
about action url: mydomain/about
OtherController actions: index
index action url: mydomain/other
MyCode That not works:
routes.MapRoute(
"Other",
"{controller}/{action}/{id}",
new { controller = "Other", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Thanks
For your Home Controller, to hit
index action url: mydomain
you need
routes.MapRoute(
name: "Home",
url: "",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
to hit
about action url: mydomain/about
you need
routes.MapRoute(
name: "Home",
url: "about",
defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional }
);
For your Other Controller, to hit
index action url: mydomain/other
you need
routes.MapRoute(
name: "Other",
url: "other",
defaults: new { controller = "Other", action = "Index", id = UrlParameter.Optional }
);
Note that, in all cases, the "name" parameter doesn't really matter.
I think you missed {controller} in your Default routing spec.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);