是否确定要发送301重定向与动作过滤器? [英] Is it OK to send a 301 redirect with an action filter?

查看:146
本文介绍了是否确定要发送301重定向与动作过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个新的asp.net MVC应用程序,我一直跟我的允许用户发布短,简洁的URL的内容,他已发布的概念玩弄。这些的短网址将在狭窄的空间像Twitter和评论领域得心应手。我喜欢这个主意,因为我不是URL的一个巨大的风扇缩短服务,因为他们是如此模糊,你永远不会真正知道你会得到什么。而是采用了短网址我想给我的客户发布的能力:

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

该做301重定向:

http://domain.com/2009/08/10/this-is-the-content-title

现在,这是一对夫妇额外的线路和一个自定义的ActionResult一个pretty简单的过程。我实现自定义的ActionResult是一个RedirectToRouteResult扩展方法......这是相当简单,但约20 code线仍然。我打得四处做相同的功能,只是这一次与ActionFilter。我的行为过滤器是这样的:

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;
    }
}

和我的操作方法看起来像(我删除了一堆code的简化):

and my action method looks like (I removed a bunch of code to simplify):

[PermanentRedirect]
public ActionResult ShortUrl(int id)
{
    return RedirectToAction("Post", id);
}

我的问题是这样的:我错过了什么或者是这个简单的?我发现,人们正在寻找做同样的事情的一些其他职位,他们总是创建一个自定义的ActionResult。除了利用总体code少,因为这种行为可能需要的其他行动方法用于其他地方,我不明白为什么它不应该是一个ActionFilter。有了这样说我是相当新的请求和响应对象,所以我不知道如果我失去了一些东西。

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.

推荐答案

您已经显示的code将工作得很好。不过,我建议您使用自定义操作的结果,而不是使用动作过滤器。

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.

另外一个原因是,code将清洁剂:将有其他开发商少的事情(或者你在2周内)看或理解。看一个简单的返回PermanentRedirectToAction(邮报,ID)是不是查看属性的,更容易的返回数据。

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.

这篇关于是否确定要发送301重定向与动作过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆