为什么总是虽然的ViewResult或PartialViewResult定义一个ActionResult的返回类型返回 [英] Why is always an ActionResult as return type defined although a ViewResult or PartialViewResult is returned

查看:438
本文介绍了为什么总是虽然的ViewResult或PartialViewResult定义一个ActionResult的返回类型返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2的典型样本:

public ActionResult Index()
{
   return View();
}

[HttpGet]
public ActionResult Create()
{
   return PartialView();
}

为什么他们不喜欢的ViewResult和PartialViewResult返回最合适的类型?

Why do they not return the most appropriate type like ViewResult and PartialViewResult?

推荐答案

当然,他们可以有一个的ViewResult PartialViewResult 在这种情况下<返回类型的 / em>的。但究竟会是给什么好处吗?如果以后你决定,在某些情况下(例如,当请求来自一个Ajax调用)要返回 JsonResult

Of course they can have a ViewResult or PartialViewResult return type in this case. But what benefit exactly would it give to you? What if later you decide that in some case (e.g. when the request comes from an Ajax call) you want to return JsonResult?

您通常不处理的操作方法自己的返回值。它是由MVC框架在幕后进行处理,所以你真的不关心签名。

You typically do not handle the return value of an action method yourself. It's processed behind the scenes by the MVC framework, so you really don't care about the signature.

这篇关于为什么总是虽然的ViewResult或PartialViewResult定义一个ActionResult的返回类型返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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