I’m using ASP.NET MVC 3.
I’ve wriiten a helper class as follows:
public static string NewsList(this UrlHelper helper)
{
return helper.Action("List", "News");
}
And in my controller code I use it like this:
return RedirectToAction(Url.NewsList());
So after the redirect the link looks like this:
../News/News/List
Is there an alternative to RedirectToAction? Is there a better way that I need to implement my helper method NewsList?
Actually you don’t really need a helper:
or if you want to avoid hardcoding:
and then:
or another possibility is to use MVCContrib which allows you to write the following (personally that’s what I like and use):
or yet another possibility is to use T4 templates.
So it’s up to you to choose and play.
UPDATE:
and then:
UPDATE 2:
Example of unit test for the
NewsListextension method: