了解MVC中的[HttpPost],[HttpGet]和复杂Actionmethod参数 [英] Understanding [HttpPost], [HttpGet] and Complex Actionmethod parameters in MVC

查看:322
本文介绍了了解MVC中的[HttpPost],[HttpGet]和复杂Actionmethod参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MVC的设计模式和框架都非常陌生.我也不是非常了解ASP.NET Forms的基础知识.但是,我确实了解Web开发以及HTTP Post和GET的基础.

I am very very new to MVC the design-pattern and also the Framework. I am also not extremely well- versed in fundamentals of ASP.NET Forms. However, I do understand the basics of web development and HTTP Post and GET as well.

现在,我一直在阅读一些MVC教程,尽管我掌握了MVC模式的工作原理以及路由引擎"的工作原理,但我还是很了解.然后突然我遇到了一个看起来像下面这样的代码:

Now, I have been going through some MVC tutorials and I though I was getting a good hold of how MVC the pattern works and also how "Routing Engine" works. Then suddenly I came across a code which looks like folloing:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View(new MyViewModel());
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return Content("Thanks", "text/html");
    }
}

我对此没有疑问:

  • 我对路由引擎的理解是,将控件传递给基于URL的特定的 ActionMethod,通常URL基本上是Controller/ActionMethod/Id类型,其中对行为有影响的参数是Primitive类型.在上面的示例中,调用"
  • My understanding of routing engine was that the control is passed to a particular ActionMethod based on URL and normally URL are basically Controller/ActionMethod/Id kind where paramter to action menthod are rather Primitive types. In this example above what kind of URL would it take to call "

公共ActionResult索引(MyViewModel模型)?"

public ActionResult Index(MyViewModel model)?"

由于NyViewModel是复杂类型,因此您不能将其作为URL的一部分传递.你怎么称呼它?

Since NyViewModel is a complex type, you can not pass it as part of URL. How can you call it?

  • 当第一个方法不需要任何属性时,为什么第二个方法用[HttpPost]装饰?是否有关于何时使用[Http]属性的准则?

我认为我在拼图中缺少很大的优势,而且两个问题都是相互关联的.但是,在关系上需要一些帮助

I think I am missing a big pice in the puzzle and both the questions are interrelated. However, need some help in making sense with the relationship

推荐答案

[HttpPost]属性告诉路由引擎将对该操作方法的任何POST请求发送到另一方法.这是一种超载.

The [HttpPost] attribute tells the routing engine to send any POST requests to that action method to the one method over the other. This is a type of overloading.

当第一种方法不需要任何属性时,为什么第二种方法用[HttpPost]装饰?

方法的默认值为[HttpGet].因此,不需要属性.

The default for a method is [HttpGet]. Because of that, no attribute is needed.

是否有关于何时使用[Http]属性的准则?

理想情况下,属性应位于每个方法上,以避免混淆.随着您对事物的工作方式更加熟悉,您通常会采用快捷方式(与其他所有方式一样),并在知道它们不必要时将其省略.

Ideally, attributes should be on every method, in order to avoid confusion. As you get more familiar with how things are working, you will often take shortcuts (as with everything else), and omit them when you know that they are not necessary.

由于MyViewModel是复杂类型,因此您不能将其作为URL的一部分传递.你怎么称呼它?

Since MyViewModel is a complex type, you can not pass it as part of URL. How can you call it?

数据将从请求正文中的数据转换为模型.这可以作为JSON对象,也可以作为表单数据来. (有一些技巧可以从URL初始化对象,但是可能有些复杂和高级.)

The data will be turned into the model from the data in the body of the request. This can come either as a JSON object, or as Form data. (There are tricks to get the object initialized from the URL, but they can be a little complicated and advanced.)

这篇关于了解MVC中的[HttpPost],[HttpGet]和复杂Actionmethod参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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