简体   繁体   English

什么是ASP.NET MVC应用程序中的“发布”?

[英]What is “posted” in ASP.NET MVC applications?

As I review more code and blog posts from lots of MVC sources, I still haven't wrapped my mind around what is "posted" when a request is made. 当我查看来自许多MVC来源的更多代码和博客文章时,我仍然没有对发出请求时“张贴”的内容有所了解。 I realize MVC doesn't support post, but I'm having trouble finding resources that can explain it well enough to understand. 我意识到MVC不支持发布,但是我很难找到可以充分解释它的资源。

Inside the controller's public ActionResult nameOfAction(what the heck goes here?) { ... } what are my parameters? 在控制器的public ActionResult nameOfAction(what the heck goes here?) { ... }我的参数是什么?

Sometimes it looks like Visual Studio scaffolds (int id, MyObject myobject) for an Edit-style action--it includes something from my model, but not always. 有时,它看起来像是Visual Studio脚手架(int id, MyObject myobject)用于编辑样式的操作-它包括我模型中的内容,但并非总是如此。

Sometimes, it's (int id, FormCollection collection) for a delete-style action. 有时,它是(int id, FormCollection collection)用于删除样式的操作。 Why not use the modeled object here? 为什么不在这里使用建模对象? Is a FormCollection object always "posted"? 是否始终将FormCollection对象“发布”?

Sometimes, I see (RouteInfo routeInfo) which isn't recognized in my MVC2 Intellisense (is this MVC1 only or something?) 有时,我看到(RouteInfo routeInfo)在我的MVC2 Intellisense中无法识别(仅此MVC1还是其他?)

How can/do/should I establish these parameters? 我如何/应该/应该如何建立这些参数? I think this will help me a lot at design time. 我认为这将在设计时对我有很大帮助。

What gets post back from a form in MVC is the form data which includes each form element in a keyvalue pair. 从MVC中的表单回发的是表单数据,该数据包括键值对中的每个表单元素。

If you only need this information then you would use: 如果仅需要此信息,则可以使用:

public ActionResult nameOfAction(string name, string lastName, string etc)

MVC has some smart data model binding which takes the form data and automatically creates an object which is part of you domain model. MVC具有一些智能数据模型绑定,该绑定采用表格数据并自动创建一个对象,该对象是域模型的一部分。 For instance it could automatically create a Person object with the provided form data. 例如,它可以使用提供的表单数据自动创建一个Person对象。

I believe there is a security concern with this as a user of your site may post data which is not part of your form and guess what your models are to inject their own data. 我认为这与安全性有关,因为您网站的用户可能会发布不属于您表单的数据,并猜测您的模型将注入自己的数据。 I dont think this is a massive issue though and this is the way I would go. 我不认为这是一个大问题,这就是我要走的路。

I think you can use the anti-forgery helper to prevent users from posting back data which is not allowed in a form. 我认为您可以使用反伪造帮助程序来防止用户回发表单中不允许的数据。 anti-forgery 反伪造

Use strongly typed views with a view-model and the strongly typed helpers. 将强类型视图与视图模型和强类型助手一起使用。 Then when you POST, you should get an instance of that view-model out. 然后,在POST时,应该获取该视图模型的实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM