ASP.Net MVC Routing: How to catch the root of a website? - asp.net-mvc-3

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...

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

MVC 3 route mapping

I have 2 routes mapped in my mapping...
this is to allow a user to type in the url with an optional parameter to quick load their town in the home page of the website, example:
www.mysite.com/manchester
www.mysite.com/liverpool
or to simply go to the defaul home page if www.mysite.com is entered with nothing else.
With the default mapping in place to handle the controller/action/parameter i have added an additional route so the parameter is handed:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }); // Parameter defaults
routes.MapRoute(
"HomePageQuickFind",
"{quickFind}",
new { controller = "Home", action = "Index", quickFind = UrlParameter.Optional });
I am not very good with route mapping as I am struggling to understand it and my question is this a bad approach which my cause "greedy routing" and is there another way of implementing my scenario?
I think you need to replace the default route with a set of routes for each of your controllers, then add your quick find route as the last route. This should allow any unmatched routes to fall through to the quick find route. Try something like this:
// Routes for standard controllers
routes.MapRoute(
"Home",
"home/{action}/{id}",
new { controller = "home", action = "index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Map",
"map/{action}/{id}",
new { controller = "map", action = "index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Default",
"users/{action}/{id}",
new { controller = "users", action = "index", id = UrlParameter.Optional }
);
// Route for www.mysite.com/cityname
routes.MapRoute(
"QuickFind",
"{quickFind}",
new { controller = "home", action = "index", quickFind = UrlParameter.Optional }
);

How can my route use optional parameters in the middle of the URL using ASP MVC3?

I would like my URLs to use the convention:
/{controller}/{id}/{action}
rather than
/{controller}/{action}/{id}
I tried setting up a route as follows:
routes.MapRoute(
"Campaign",
"{controller}/{action}/{id}",
new { controller = "Campaign", action = "Index", id = UrlParameter.Optional }
);
But this doesn't work because I am unable to make the id parameter optional.
The following URLs do work:
/campaign/1234/dashboard
/campaign/1234/edit
/campaign/1234/delete
But these URLs do not:
/campaign/create
/campaign/indexempty
MVC just calls Index for both. What am I doing wrong?
I think you probably need two separate routes for this.
routes.MapRoute(
"CampaignDetail",
"{controller}/{id}/{action}",
new { controller = "Campaign", action = "Index" }
);
routes.MapRoute(
"Campaign",
"{controller}/{action}",
new { controller = "Campaign", action = "Index" }
);

How do I redirect everything to a single controller?

I have three specific routes:
routes.MapRoute(
"Home Page",
"",
new { controller = "Home", action = "Index" }
);
routes.MapRoute(
"Admin Section",
"AdminSection/{action}/{id}",
new { controller = "AdminSection", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"Listings",
"{controller}/{action}/{id}",
new { controller = "Listings", action = "Index", id = UrlParameter.Optional }
);
Basically, the first two routes work as planned, however, I want everything that isn't specifically in a route to be redirected to the listings controller.
I am still quite new to routing and have been trying to Google this for the past hour without any luck - I know exactly what is going on here, but, I don't know how to fix it.
I have used RouteDebugger, and I can see that it is hitting the route, but, the issue is that it will only go to the Listings controller if a controller is not specified - but, obviously there will always be something there.
I have tried a few different combinations - I thought I was on to a winner by removing the {controller} part of the URL and still defining the default value, but, I am not having much luck.
Does anyone know what I need to do?
How about this:
routes.MapRoute("Listings", "{action}/{id}",
new { controller = "Listings", action = "Index", id = UrlParameter.Optional });
site.com/test :
It'll go to action: test, controller: listing, id = blank
Edit: As I understand it you want a catch-all route.
http://richarddingwall.name/2008/08/09/three-common-aspnet-mvc-url-routing-issues/
routes.MapRoute("Listings", "{*url}",
new { controller = "Listings", action = "Index" }
);
Original:
I can't test this at the moment but
routes.MapRoute(
"Listings",
"{anythingOtherThanController}/{action}/{id}",
new { controller = "Listings", action = "Index", id = UrlParameter.Optional }
);
This should work.
In your Listings controller, just accept a string parameter "anythingOtherThanController" and it will get bound to it.
The main problem here is that /some/action will be mapped to the same action as /another/action. So I'm not sure what you're trying to do here :)
Provide a default route and provide controller name as listings controller. Keep this route mapping at the bottom of all the mappings.
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Listings", action = "Index", id = UrlParameter.Optional }
);
Sorry I got sequence mixed.

ASP.NET MVC 3.0 Url Rewritng

I have Action called Contact in home controller
<mysite>/Home/Contact
I want to be able by typing <mysite>/Contact to get the same result as <mysite>/Home/Contact
Is it possible to do with mvc 3.0 routes or RouteMagic?
Currently i am trying to achieve this like that, but no luck:
Custom Routes:
routes.MapRoute(
"Contact", // Route name
"Contact", // URL with parameters
new { controller = "Home", action = "Contact", id = UrlParameter.Optional } // Parameter defaults
);
RouteMagic:
var route = routes.MapRoute("new", "Contact");
routes.Redirect(r => r.MapRoute("old", "Home/Contact"))
.To(route);
Update
Ok the custom routes should be defined first, now it is working(in case of custom routes), but there is appeared a new question why route magic returning error:
Server Error in '/' Application.
Value cannot be null or empty.
Parameter name: controllerName
Make sure your new route occurs before the default route (since it will match as well) when defining the route.
routes.MapRoute(
"Contact", // Route name
"contact", // URL with parameters
new { controller = "Home", action = "Contact", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Contact", id = UrlParameter.Optional } // Parameter defaults
);
have you tried the rewrite module in iis7 ?
its easy to use , donwlod it from here:
http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/

Resources