MVC 3 route mapping - asp.net-mvc-3

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

Related

Creating a simple site to display my apps, having some routing issues

I want to create a site where I display my apps. I use MVC 3.
My Home controller is a typical overview page where I'll list all my apps. Then there is the App controller, where if you click on a app on a overview could get more info... url scenario would be:
http://myhost.com <-- overview
http://myhost.com/App/app_name <-- render info about that app
But I'm having a small problem understanding routes, thought i did it right:
routes.MapRoute(
"App", // Route name
"{controller}/{id}", // URL with parameters
new { controller = "App", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
And how do I get a hold of id in the controller?
Would your app rule not be more specific, that is
routes.MapRoute(
"App", // Route name
"App/{id}", // URL with parameters
new { controller = "App", action = "Index",
id = UrlParameter.Optional }
// Parameter defaults
);
Note replaced {controller} with App

#Html.ActionLink not generating link

route :
routes.MapRoute("Default", "{controller}/{action}/{id}/{args1}/{args2}/{args3}", // URL with parameters
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional,
args1 = UrlParameter.Optional,
args2 = UrlParameter.Optional,
args3 = UrlParameter.Optional
}
);
Creating Link using below code:
#Html.ActionLink("Photos", "List", "Photos");//"photos" is controller name and "list" is action name
its generating anchor but url/link is blank.
I have modified Route as i required some extra parameters for some actions.
I am new to MVC, Please provide me solution.
Seems like default route is not active, if so then activate it.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

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

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