I have an ASP.NET MVC applications and i don't want to explicitly write the actions or the views names like this :
return RedirectToAction("Index"); or return View("Home");
what is the best practice for handling those strings?
I use Visual Studi开发者_如何学Co 2010 and ASP.NET MVC2
There's MVCContrib and inside there are extension methods allowing you to write:
return RedirectToAction<HomeController>(x => x.About());
Another possibility is T4 templates.
Use T4MVC. It will allow you to kill all magic strings in your code.
I've created a static class like so:
public static class Routing {
public static RouteValueDictionary Index {
get {
return new RouteValueDictionary {
{ "controller", "Default" },
{ "action", "Index" }
};
}
}
}
so i can use it like this:
return RedirectToAction(Routing.Index);
精彩评论