Managing routes/Global.asax maintainability - asp.net-mvc

Is there any best practice for how to best define and organize routes in MVC?
The company I work for runs a very extensive, complex ecommerce site with ~600K unique visitors/day.
Here's the problem: in our Global.asax.cs, we've got this HUGE list of approximately 75 route definitions in our RegisterRoutes():
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Is there a better way to define these routes other than having this gigantic list in the Global.asax.cs?
Because we've got a bunch of developers and half of them are incompetent and I can't go back refactoring these routes, it can take literally a couple minutes to figure out what controller is responsible for delivering a URL's View.
What can I do?
One developer toiled away building a prototype that allows us to do this in our Global.asax.cs:
public static void RegisterRoutes(RouteCollection routes)
{
routes.Include(new RootController());
routes.Include(new AccountController());
routes.Include(new HelpController());
routes.Include(new SearchController());
// etc., for each controller
}
In this prototype, Include() is an extension method and all Controllers inherit from IRoutedController, which provides Include() an IEnumerable<Route> list of Routes to add to the RouteCollection.
But with this prototype, we have a new problem: instead of looking through a list of route.MapRoute() calls to find which controller a specific URL invokes, we now have to guess which Controller is responsible for a specific URL and check its IRoutedController list of routes to see if the URL actually invokes that Controller we guessed. Not so hard, but sometimes takes just as long as examining our list of 75+ Routes in Global.asax.cs.
Is this a better solution?
Is there any good solution?
Should we just keep adding routes to Global.asax.cs; should we give the prototype the green light; or should we do something else? (Assume that you cannot refactor existing route URLs to make them more logical.)

I'm sorry, but what are you talking about? :P
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
This single route entry effectively allows you to call Any action method of Any controller with any parameters.
Is this a better solution?
Probably not.
Is there any good solution?
Yes. But you need to share more of your routes to be sure.
Should we just keep adding routes to Global.asax.cs; should we give the prototype the green light; or should we do something else?
No, No, and Yes. You need to share a decent amount of routes to have a better idea of the situation.
(Assume that you cannot refactor existing route URLs to make them more logical.)
But you can make sure new ones fit in the default route, so the problem doesn't keep growing i.e. {controller}/{action}

I would have stored the route information externally in xml file or database and loaded it in global.asax. I can add extra column/attribute that will have example url being routed so that I can search it quickly. Not to mention, I can update route information w/o rebuilding the project (of course, if there are new controllers, views etc then hey would need to be packaged in a new dll).

Related

make url shorter of some controllers and actions

I would like to make some urls of my asp.net MVC4 app shorter. For example I have Account controller and such action ForgotPassword. Url looks like this
http://www.mydomain.com/account/forgotpassword
I would like to make the url shorter(example below) without renaming actual controller and action names. What is the best way to do that?
http://www.mydomain.com/a/fp
You could register a simple route:
routes.MapRoute(
name: "ForgottenPassword",
url: "a/fp",
defaults: new { controller = "Account", action = "ForgottenPassword" }
);
...in RouteConfig.cs if you're using MVC4.
If i am not wrong, then your talking about Friendly URL's?
Please have a look # quysnhat.wordpress.com
There was a very nice post in Hanselman's web.
Also, there were few questions related to friendly url's(in case it helps you) :-
How can I create a friendly URL in ASP.NET MVC?
http://www.intrepidstudios.com/blog/2009/2/10/function-to-generate-a-url-friendly-string.aspx
The easiest but not the best way of doing it is to hand-code your custom routes:
routes.MapRoute(
name: "AccountForgotRoute",
url: "a/fp/{id}",
defaults: new { controller = "Account", action = "ForgotPassword", id = UrlParameter.Optional }
);
The downside to that is if you will have tons of controllers and action methods and you like all of them to be "shortened", then you will have to write a lot of routes.
One alternative is to define your custom routes in a database and write it out on a file. So for example you have in a database row an accountcontroller-forgotpassword key with a value of a/fp, you dynamically build the route definition, write it in a file and let your application pick it up. How your application can pick up the route definition can be done like this. That link is still applicable for MVC 4. But this one is really messy, IMO, but is an alternative.

ASP.NET MVC: Many routes -> always only one controller

I have very simple question. My site, based on ASP.NET MVC, can have many urls, but all of them should bring to the one controller. How to do that?
I suppose I need some magic in Global.asax but I don't know how to create route that will redirect any url to the specific controller.
For example I have url /about, /product/id etc. but all of them should be really bring to the content/show where the parts of url will be recognized and the decision what information to show will be make. It's some like CMS when you cannot define routes in advance. Is this information enough?
Thanks
This sounds like a horrible idea, but, well, if you must;
routes.MapRoute(
"ReallyBadIdea",
"{*url}",
new { controller = "MyFatController", action = "MySingleAction" }
);
This routes everything to a single action in a single controller. There's also {*path} and other URL patterns should you want slightly more flexibility.
Ideally you should try and specific with your routes, for example if you have a URL that is /products/42 and you want it to go to a generic controller you should specify it explicitly like
routes.MapRoute(
"Poducts",
"products/{id}",
new { controller = "Content", action = "Show", id = UrlParameter.Optional }
);
then you would specify another route for something else like /customers/42
routes.MapRoute(
"Customers",
"customers/{id}",
new { controller = "Content", action = "Show", id = UrlParameter.Optional }
);
this may seem a little verbose, and creating a single route might seem cleaner, but the issue a single route is you will never get a 404 and will have to handle such things in code.

Duplicate content in ASP.NET MVC because of custom routes MapRoute(), are areas the rescue?

I use custom routes for my URLs and my action become accessible via two URLs (not counting trailing slash and lower\upper case letters): one via my custom route /my-custom-route-url/ and one via default /controller/action.
I see one possible solution -- put all controllers which use default routing (they are mostly backend) in one area, and place all others in separate area and use it without default route.
May be there is a better way?
The Default Route is an unnecessary evil in my opinion. It's helpful in that it means you write less code, but really I find it's far better to simply create each or at least most routes by hand.
Areas are a good way to separate different parts of your application, but using it just to avoid dealing with a blank default route isn't the correct reason. I use areas to isolate my admin area from my default area, but my reasoning for doing so is that they are two independent systems.
If you like you can make default routes for each controller as needed on a case by case basis. For example you could have a default products/{action} route and a default home/{action} route, but then individually define each route in the ContactController or what have you.
routes.MapRoute(
"Products_Default",
"products/{action}",
new {controller = "Products", action = "Index"},
new[] {"Web.Components.Controllers"}
);
routes.MapRoute(
"Contact_Send",
"contact/send",
new {controller = "Contact", action = "SendMessage"},
new[] {"Web.Components.Controllers"}
);
routes.MapRoute(
"Contact_Home",
"contact",
new {controller = "Contact", action = "Index"},
new[] {"Web.Components.Controllers"}
);
IMHO, this is duplication and should be avoided. You could use URL Rewriting of IIS to do this with its rewrite module.
The custom URL can be rewritten at the IIS level to use the normal controllers and actions and I think that should serve your purpose.
For more info look here: http://learn.iis.net/page.aspx/460/using-url-rewrite-module/

Avoiding the Controller with Routing Rules in ASP.NET MVC

I've created a website with ASP.NET MVC. I have a number of static pages that I am currently serving through a single controller called Home. This creates some rather ugly URLs.
example.com/Home/About
example.com/Home/ContactUs
example.com/Home/Features
You get the idea. I'd rather not have to create a controller for each one of these as the actions simply call the View with no model being passed in.
Is there a way to write a routing rule that will remove the controller from the URL? I'd like it to look like:
example.com/About
example.com/ContactUs
example.com/Features
If not, how is this situation normally handled? I imagine I'm not the first person to run in to this.
Here's what I've done previously, using a constraint to make sure the shortcuts don't conflict with other routing rules:
routes.MapRoute(
"HomeShortcuts",
"{action}",
new { controller = "Home", action = "Index" },
new { action = "Index|About|ContactUs|Features" }
);
Add defaults for the controller names in the new statement. You don't have to have {controller} in the url.

Asp.Net MVC routing: best way to have a single element in the URL?

I will take the example of the SO site. To go to the list of questions, the url is www.stackoverflow.com/questions. Behind the scene, this goes to a controller (whose name is unknown) and to one of its actions. Let's say that this is controller=home and action=questions.
How to prevent the user to type www.stackoverflow.com/home/questions which would lead to the same page and would lower the rank of the page as far as SEO is concerned. Does it take a redirect to solve this? Does it take some special routing rules to handle this kind of situation? Something else?
Thanks
I assumed that the controller was questions and the action was index, i.e., the default action as defined by the route handler. Thus there isn't an alternative path to the page.
During Phil Haack's presentation from PDC, Jeff shows some of the source code for Stack Overflow. Among the things he shows is the code for some of the route registrations. He's got these in the controllers, and it's not clear to me that he uses a default route at all. With no default route, you wouldn't need to worry about /home/questions, for example.
As for /questions/index, yes, a permanent redirect is the way to go. You won't get any search engine penalty for a permanent redirect.
Another way to eliminate /home/questions would be to use a route constraint.
You want to use the following route. It is really easy you just create a new route that eliminates the need for the controller to be in the route. You create a template string that just contains the action and you default the controller to the controller you want to use such as "Home".
routes.MapRoute(
"MyRoute",
"{action}",
new { controller = "Home", action = (string)null },
new { action = "[a-zA-z_]+" }
);
Hope this helps.

Resources