ASP.Net MVC - HTTP 状态代码(即 303、401、404 等)
在 ASP.Net MVC 中,是否可以使用 Redirect
或 RedirectToAction
来调用 303 错误等?
我正在阅读一本名为“ASP.NET MVC 1.0 Website Programming”的书,源代码正在进行诸如 return this.Redirect(303, FormsAuthentication.DefaultUrl);
之类的调用,但此调用仅起作用对于外部库,如果可能的话,我希望拥有相同的功能而无需附加组件。
In ASP.Net MVC is it possible to use either Redirect
or RedirectToAction
to call for example a 303 error?
I am working my way through a book titled "ASP.NET MVC 1.0 Website Programming" and the source is making calls such as return this.Redirect(303, FormsAuthentication.DefaultUrl);
but this call only functions with an external library, I would like to have the same functionality sans the add-on if possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以创建自定义 ActionResults 来模仿您想要的任何 http 响应代码。通过返回这些操作结果,您可以轻松执行 303。
我发现 这篇快速文章,您应该能够轻松理解。
You can create custom ActionResults that mimic any http response code you'd like. By returning those action results, you can easily perform a 303.
I found this quick write-up that you should be able to follow easily.
这是我根据当前答案中给出的建议和反编译的 System.Web.Mvc.RedirectResult 代码得出的结论:
Here's what I came up with based on the advice given in the current answers and the decompiled
System.Web.Mvc.RedirectResult
code:关于此的两种方法-
-或-
请注意,在第一个重定向中, false 参数避免了 redirect(url) 通常引发的 threadAbort 异常。这是使用这两种方法之一的充分理由。
2 ways about this-
-or-
Note that in the first redirect, that the false parameter avoids the threadAbort exception that the redirect(url) normally throws. This is one good reason to use one of these 2 methods.
您还可以使用自定义 ActionFilter 来完成它,就像我提到的 此处。我个人更喜欢 ActionFilter 而不是 ActionResult。
You could also accomplish it with a custom ActionFilter like I mention here. I for one like the ActionFilter a bit more the the ActionResult.