ASP.NET MVC:奇怪的POST行为 [英] ASP.NET MVC : strange POST behavior

查看:194
本文介绍了ASP.NET MVC:奇怪的POST行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.NET MVC的 2 应用

ASP.NET MVC 2 app

我有我的控制器(的Toons)上的两个动作:

I have two actions on my controller (Toons):


  1. [GET]列表

  2. [POST]添加

应用是在IIS7集成模式下运行,因此/的Toons /表工作正常。但是当我做POST(即重定向到/的Toons /列表内部)重定向(含302对象移动)回/的Toons /添加。

App is running on IIS7 integration mode, so /Toons/List works fine. But when I do POST (that redirects to /Toons/List internally) it redirects (with 302 Object Moved) back to /Toons/Add.

问题消失,如果我使用的.aspx黑客(在IIS6 / IIS7经典模式下工作)。

The problem goes away if I use .aspx hack (that works in IIS6/IIS7 classic mode).

但是,如果没有的.aspx - GET做工精细,但POST重定向我拉到本身,而是用GET

But without .aspx - GET work fine, but POST redirects me onto itself but with GET.

我在想什么?

我与webhost4life.com托管和他们没有改变IIS7已经集成模式。

I'm hosting with webhost4life.com and they did change IIS7 to integrated mode already.

编辑:的code作为使用UltiDev卡西尼服务器预期工作

The code works as expected using UltiDev Cassini server.

编辑:原来是尾随斜杠在URL的问题。不知怎的,IIS7不路由请求正确,如果有结尾没有斜杠。

It turned out to be trailing-slash-in-URL issue. Somehow IIS7 doesn't route request properly if there is no slash at the end.

EDET:行为结果的说明
当我要求什么情况是(POST) /的Toons /列表(没有后面的斜杠),IIS没有找到处理程序(我没有知识,了解如何准确IIS的确URL到处理程序映射)和请求(使用302 $ C $三)重定向 /的Toons /列表/ (注意斜杠)。

EDET: Explanation of the behavior
What happens is when I request (POST) /Toons/List (without trailing slash), IIS doesn't find the handler (I do not have knowledge to understand how exactly IIS does URL-to-handler mapping) and redirects the request (using 302 code) to /Toons/List/ (notice trailing slash).

有一个浏览器,根据HTTP规范,必须将请求重定向用同样的方法(POST在这种情况下),而是它可以处理302好像是303和问题查询请求新的URL。

A browser, according to the HTTP specification, must redirect the request using same method (POST in this case), but instead it handles 302 as if it is 303 and issues GET request for the new URL.

这是大多数浏览器的不正确,但已知的行为。

This is incorrect, but known behavior of most browsers.

解决方法是,使用的.aspx乱砍,使之明确为IIS如何请求映射到ASP.NET处理程序,或配置IIS来处理使用ASP.NET处理程序中的虚拟目录的一切。

The solution is either to use .aspx-hack to make it unambiguous for IIS how to map requests to ASP.NET handler, or configure IIS to handle everything in the virtual directory using ASP.NET handler.

问:什么是更好的方式来处理这个问题。

Q: what is a better way to handle this?

推荐答案

您必须对您的code控制。
更改,恕不尾随斜线后张贴到正确的页面的所有页面。
如果第三方客户端,比返回一个例外,那他们应该修复的bug。

You have control over your code. change all pages that make a post without the trailing slash to post to the correct page. if its 3rd party clients, than return an exception, that they should fix the bug.

这是预期的行为,它不是你的工作,恢复一切都有可能发生。
但你应该给很好的提示(例如异常消息,而不是一个奇怪的错误或重定向)。

this is expected behaviour, and its not your job to recover everything that can happen. but you should give good hints (e.g. exception message, instead of a weird error or redirect.)

这篇关于ASP.NET MVC:奇怪的POST行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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