开发者

MVC 2 - URL Shortening/ Routing (Map Route)

I have a URL: http://localhost:XXXX/Details/569

I want to shorten it to: http://localhost:XXXX/569

Currently I have:

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
开发者_JAVA技巧

So what changes do I do in my routes so that when an integer value is entered, it goes to my Details function in the HomeController:

public ActionResult Details(int recordID)
{
    /** Code Here **/
    return View();
}

EDIT: Catching Errors

Also how can I catch for any error that happens in the application? Rather than display an error dump, I would like to show a "nice" message that says, "Oops! Something Broke!".


Add this route before the default one. It will make sure the URLs containing only numbers calls HomeController's Details action.

routes.MapRoute(
    "RecordDetails",
    "xxxx/{RecordID}",
    new { controller = "Home", action = "Details", RecordID=0 },
    new { RecordID = @"\d+" });
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜