我怎样才能学会停止担心并爱上 ASP.NET MVC Forms?
MVC 鼓励 RESTful URL,但 HTML 表单本质上会将数据附加到查询字符串值中。我的操作将“文本”作为字符串参数。我的形式是:
<% using(Html.BeginForm("Action", "Controller")) { %>
<%= Html.TextBox("text") %>
<input type="submit" value="submit" />
<% } %>
我的操作是:
public ActionResult Action(string text)
{
...
return toInnocence;
}
您的操作可以映射到两个不同的 URL:
~/Controller/Action/textvalue
感谢您的路线图 {controller}/{action}/{text 我- 从表单提交时的 ~/Controller/Action?text=textvalue
的问题是:
如何区分两种表单并在后一种情况下进行重定向?第二种形式打破了 RESTful 原则。那里的最佳实践是什么?我不想查询 RouteData.Values 集合,因为它破坏了以自然、直接的方式将请求参数映射到函数参数的整个目的。这是一个非常基本的场景,我希望 MVC 能够很好地处理这个问题。
第二种形式不映射到控制器操作中的“text”参数。为什么?那么如何创建同一操作的重载版本呢?我是否必须创建一个新操作并将其用于表单提交?当然,我可以解决所有这些问题,但同时我担心在某个地方错过大局。
看起来人们很轻松地处理这些问题,所以我觉得我是唯一一个对路由值与查询字符串感到困惑的人。
编辑:我查看了维基百科是如何做到这一点的。它对“表单 getter”和实际的静态 URL 使用单独的操作,并根据需要从一个重定向到另一个。我想这将是最好的方法。
MVC encourages RESTful URL's, yet HTML forms by nature append the data in query string values. My action takes "text" as a string parameter. And my form is:
<% using(Html.BeginForm("Action", "Controller")) { %>
<%= Html.TextBox("text") %>
<input type="submit" value="submit" />
<% } %>
My action is:
public ActionResult Action(string text)
{
...
return toInnocence;
}
There are two distinct URL's that your action can map to:
~/Controller/Action/textvalue
thanks to your route map {controller}/{action}/{text}~/Controller/Action?text=textvalue
when submitted from a form
My question is:
How can I differentiate between two forms and do a redirect in the latter case? The second form breaks RESTful principle. What's the best practice there? I don't want to query RouteData.Values collection because it breaks the whole purpose of mapping request parameters to function arguments in a natural, straightforward way. This is a very basic scenario I expect MVC to handle this nicely.
The second form doesn't map to "text" parameter in the controller action. Why? How can I create overloaded versions of the same action then? Do I have to create a new action and use it for form submissions? Of course I can workaround all these but at the same time I'm afraid of missing the big picture somewhere.
It looks like people are getting along with these at ease so I feel like I'm the only one who is confused by route values against query strings.
EDIT: I looked at how Wikipedia does this. It uses separate actions for "form getter" and the actual restful URL and redirects from one to another as needed. I guess that would be the best way of doing it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
浏览器的默认操作是将 GET 形式的值放入查询字符串中。他们都会决定一条路线并采取相同的行动。
由于默认路由的存在,这将正确解析。
我能想到的做你所要求的唯一方法就是改变表格。不要提交“GET”请求,而是以 PUT 形式提交,因为您不需要 POST(您可以使用 JS 执行此操作,友好地降级为 POST)并重定向到 GET。
The default operation of your browser is to put the values in the GET form in the query string. They will both resolve to a route of and go to the same action.
Because of the presence of the default route, this will correctly resolve.
The only way I can think of to do what you're asking is to change the form. Instead of submitting a "GET" request, submit as a PUT because you don't need a POST (you can do this with JS, with a friendly degrade to POST) and redirect to a GET.