开发者

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 defau开发者_如何学Pythonlt 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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜