I'm writing a new asp.net mvc application and I've been toying with the idea of allowing my user to post short, concise urls to content that he has posted. Those short url's will be handy in cramped spaces like Twitter and comment areas. I like this idea as I'm not a huge fan of url shorteners because they're so vague and you're never really sure what you're going to get. Instead of using a url shortener I want to give my client the ability to post:
http://domain.com/p/234
which does a 301 redirect to:
http://domain.com/2009/08/10/this-is-the-content-title
Now, this is a pretty simple process with a couple of extra routes and a custom ActionResult. The custom ActionResult I implemented is an extension method on a RedirectToRouteResult... It's fairly straightforward but about 20 lines of code nonetheless. I played around with doing the same functionality, only this time with an ActionFilter. My action filter looks like:
public class PermanentRedirectAttribute : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.HttpContext.Response.StatusCode = 301;
}
}
and my action method looks like (I removed a bunch of code to simplify):
[PermanentRedirect]
public ActionResult ShortUrl(int id)
{
return RedirectToAction("Post", id);
}
My question is this: Did I miss something or is it this simple? I've found some other posts where people are looking to do something similar and they always create a custom ActionResult. Besides using less overall code, given that this behavior may need to be used elsewhere on other action methods, I don't see why it shouldn't be an ActionFilter. With that being said I'm fairly new to the Request and Response objects so I'm not sure if I'm missing something.
The code you have shown will work just fine. However, I recommend using a custom action result instead of using an action filter.
One reason for this is that it will provide more information for your unit tests since there are fewer things to verify. That is, with a custom action result you can verify that the right type of result was used and that it has the right properties set on it. With your current design you'd have to separately verify both the action result data and that you correctly applied the attribute.
Another reason is that the code will be cleaner: There will be fewer things for other developers (or you in 2 weeks) to look at or understand. Looking at a simple
return PermanentRedirectToAction("Post", id)
is much easier than looking at an attribute and the return data.