mvc3 controller not working - is path mapped wrong - asp.net-mvc-3

Here is my code:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Admin", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
}
for first link it works well if i go to:
localhost/song
localhost/date
etc. it opens all links under home controller.
But for second maproute:
localhost/admin
localhost/admin/index
- these link are not working? Can anyone please tell me what i am doing wrong?

First, your default route must be last in the list, not first.
Second, You have two default routes. There is no way for MVC to know which one to use, so it always chooses the first one that matches. Instead, your Url for the admin one should be "Admin/{action}/{id}"

Related

How to remove HOME from the url for action results other than Index

How can I replicate this default MVC route code below but to work with multiple ActionResults that are in the home controller. I want them to work just like Index where you do not need /Home/Index in the url to hit example.com/index
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
I would like to hit example.com/about and example.com/contact without needing the the controller name in the beginning.
I have tried adding that same code but replaced Index with another action method and it works but it doesn't allow you to have more than 1 existing in this structure at the same time.
Solution?
Ok so I think I got it to work after reading this thread:
ASP.NET MVC - Removing controller name from URL
In the RouteConfig I added the following right before the default route:
routes.MapRoute(
"Root",
"{action}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { isMethodInHomeController = new RootRouteConstraint<HomeController>() }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Then inside of the Controller whos name you are trying to remove from the URL you need to add this:
public class RootRouteConstraint<T> : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
var rootMethodNames = typeof(T).GetMethods().Select(x => x.Name.ToLower());
return rootMethodNames.Contains(values["action"].ToString().ToLower());
}
}
Now if I go to example.com/about , it works and I don't have to use example.com/Home/About

ASP.NET MVC3 Complex Routing Issue

I have a the following default route set up and it works fine:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Here is an example of successful route for me: "/PositionDetail/Candidates/2"
This is all fine and dandy, but the issue I have is that I want to set up a route that goes deeper. i.e. "/PositionDetail/Candidates/2/GetMoreCandidates" and "/PositionDetail/Candidates/2/Resume/3" where the Resume would be another action that I want to perform, and 3 would be ID. Note: Each of these route will load a new page, and not some partial view.
How do I set something up like this, and what would the 'PositionDetail' Controller Look like?
For example, for second task it may be as follows:
public ActionResult Resume(int CandidateID, int ResumeID)
{
return View();
}
In your Routing:
routes.MapRoute(
"Resume", // Route name
"{controller}/Candidates/{CandidateID}/{action}/{ResumeID}", // URL with parameters
new { controller = "PositionDetail", action = "Resume", CandidateID = UrlParameter.Optional, ResumeID= UrlParameter.Optional }
);
For fist task - the same logic

MVC 3 Changing Home route

It's my first MVC project and my customer doesn't want any links like xxx.com/Home/Index or something like that. When i change my controller name the browser looking for /Home and it gives me an ex. Bec there's no HomeController.
How can i change it the default controller "Home" with another one.
Just change your Default route:
Default route created in Global.asax (Unchanged)
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Modify the defaults for the parameters (controller, action, etc.). Something like this:
Modified Default route (Changed)
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "YourNewController", action = "SomeOtherAction", id = UrlParameter.Optional } // Parameter defaults
);
The routes are set in the Global.asax file in the RegisterRoutes(RoteCollection routes) routine. The default route is specified as:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
You would need to change the "Home" part to your new controller name.
You can also set up additional routes as required (see here).

passing parameters into controller's action skipping action name

I'm looking for the way to shorten the URL for getting some from DB. I've got Homecontroller and writing action, which adds rows to DB and each of them has UID. There is also search action which finds the record in DB anŠ² renders a view.
So I use localhost/Home/writing to add a record and localhost/Home/search/3456 to search a record by UID.
Is there any way to pass UID (3456) into controller's action skipping action's name, so to search a resord I can use localhost/Home/3456 or even localhost/3456?
I've tried
routes.MapRoute(
"default-action",
"{controller}/{id}",
new { action = "search", id = UrlParameter.Optional }
);
and
routes.MapRoute(
"default-action",
"{id}",
new {controller = "Home", action = "search", id = UrlParameter.Optional }
);
but got nothing.
You could try using a route constraint:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Search",
"{id}",
new { controller = "Home", action = "Search" },
new { id = #"\d+" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Now:
localhost/Home/writing will hit the Writing action on HomeController
localhost/3456 will hit the Search action on HomeController and pass it id=3456 as parameter
The numeric constrained we defined for the {id} route token is required in order for the routing engine to disambiguate between the search and the default route patterns (since action names cannot start with a number). It assumes that your identifiers are numbers.

ASP.Net MVC Routing: How to catch the root of a website?

In my global.asax I have three routes:
//MemberHome is supposed to handle urls like http://localhost/johndoe
routes.MapRoute(
"MemberHome", // Route name
"{username}",
new { controller = "PublicMember", action = "Index", username = "username" }
);
//Home is supposed to catch http://localhost/
routes.MapRoute(
"Home",
""
);
// the default way of doing things..
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The problem is, I can get them to work separtely (just home or just MemberHome)
But when they are both activated its either Home or MemberHome which gives me a 404 resource not found..
Any idea how I can get this work?
You need to set the first part of the route to something distinct. Eg:
routes.MapRoute(
"MemberHome", // Route name
"MemberHome/{username}",
new { controller = "PublicMember", action = "Index", username = "username" }
);
or:
routes.MapRoute(
"Default",
"Home/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
That way they will be distinct. Whichever one is given the fixed part of the route should come first, as otherwise the route handler will match the wildcard one first...

Resources