How define this routes in asp.net mvc4? - asp.net-mvc

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

Related

ReturnUrl is not formed properly while accessing one of my controller?

I have one controller Jobs when I'm trying to access this controller without logging to my portal, return url is not formed properly and it looks like
http://test-employer.cloudapp.net/accounts?ReturnUrl=%2F
and for accessing any other controller its formed properly
http://test-employer.cloudapp.net/accounts?ReturnUrl=%2Fusers
Below is my routing config
routes.MapRoute(
name: "JobFeed",
url: "jobs/{q}/jobfeed.xml",
defaults: new { controller = "Jobs", action = "Feed", q = UrlParameter.Optional }
);
routes.MapRoute(
name: "JobPostingFeed",
url: "postings/{q}/jobfeed.xml",
defaults: new { controller = "Postings", action = "Feed", q = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

ASP.NET MVC route

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) { /* ... */ }

How to define a route that doesn't require an action in the URL?

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 }

How can I make two MVC routes starting with different path names use the same controller in MVC4

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

Routing not working

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

Resources