ASP.NET MVC:如何将ActionResult转换为字符串?

时间:2021-09-19 21:08:07

I would like to take an existing action method, render its return value to a string and ship it as a JSON for a response to an AJAX request.

我想采用一个现有的action方法,将其返回值呈现给一个字符串,并将其作为JSON发送给AJAX请求。

To do this, I need to render an ActionResult to a string. How do i do this?

为此,我需要将ActionResult渲染为字符串。我该怎么做呢?

We have the opposite where we can convert a string to an ActionResult by using this.Content().

我们可以使用this.Content()将字符串转换为ActionResult。

Update

更新

The existing and 1st action method returns a type ActionResult but it really returns a ViewResult to respond to HTTP post request. I have a 2nd action method (my facade) that returns a JsonResult that responds to AJAX requests. I want this 2nd action method to use the 1st action method to render the HTML.

现有和第一个操作方法返回一个ActionResult类型,但它确实返回一个ViewResult来响应HTTP post请求。我有第二个动作方法(我的外观),它返回一个响应AJAX请求的JsonResult。我希望第二个动作方法使用第一个动作方法来呈现HTML。

In the grand scheme of things, I want an ActionResult (generated from an action method) retrievable not only by a standard HTTP post, but also by an AJAX request via a facade action method (the 2nd action method). This way, I, as a developer, have the choice of using an HTTP Post or AJAX to retrieve the rendering of a page.

在宏观方案中,我想要一个ActionResult(从一个动作方法生成),不仅可以通过标准的HTTP帖子检索,还可以通过Facade动作方法(第二个动作方法)通过AJAX请求检索。这样,作为开发人员,我可以选择使用HTTP Post或AJAX来检索页面的呈现。

Sorry i tried to make this update as short as possible. Thanks.

对不起,我试图让这个更新尽可能短。谢谢。

3 个解决方案

#1


6  

Are you looking for number 4 or 6 bellow?

你在找4号或6号低于标准杆?

Text extracted from here:

从这里提取的文字:

Understanding Action Results

了解行动结果

A controller action returns something called an action result. An action result is what a controller action returns in response to a browser request.

控制器操作返回称为操作结果的内容。操作结果是控制器操作响应浏览器请求而返回的内容。

The ASP.NET MVC framework supports several types of action results including:

ASP.NET MVC框架支持几种类型的操作结果,包括:

  1. ViewResult - Represents HTML and markup.
  2. ViewResult - 表示HTML和标记。
  3. EmptyResult - Represents no result.
  4. EmptyResult - 表示无结果。
  5. RedirectResult - Represents a redirection to a new URL.
  6. RedirectResult - 表示重定向到新URL。
  7. JsonResult - Represents a JavaScript Object Notation result that can be used in an AJAX application.
  8. JsonResult - 表示可以在AJAX应用程序中使用的JavaScript Object Notation结果。
  9. JavaScriptResult - Represents a JavaScript script.
  10. JavaScriptResult - 表示JavaScript脚本。
  11. ContentResult - Represents a text result.
  12. ContentResult - 表示文本结果。
  13. FileContentResult - Represents a downloadable file (with the binary content).
  14. FileContentResult - 表示可下载的文件(带有二进制内容)。
  15. FilePathResult - Represents a downloadable file (with a path).
  16. FilePathResult - 表示可下载文件(带路径)。
  17. FileStreamResult - Represents a downloadable file (with a file stream).
  18. FileStreamResult - 表示可下载文件(带有文件流)。

All of these action results inherit from the base ActionResult class.

所有这些操作结果都继承自ActionResult基类。

#2


1  

Return it as a ContentResult rather than an ActionResult

将其作为ContentResult而不是ActionResult返回

I use something like

我喜欢用的东西

    public ContentResult Place(string person, string seat)
    {
        string jsonString = null;
        try
        {

            jsonString = AllocationLogic.PerformAllocation(person, seat);
        }
        catch {
            jsonString = AllocationLogic.RaiseError(timeout);
        }
        return Content(jsonString);
    }

#3


1  

Are you sure JsonResult isn't what you want? If you call the Json(object jsonObject) method that is defined in Controller, it will serialize jsonObject into JSON and return an appropriate response (with all the headers correctly set and all that). Generally JSON requests need to be POST, but you can configure it to allow GET too.

你确定JsonResult不是你想要的吗?如果调用Controller中定义的Json(对象jsonObject)方法,它会将jsonObject序列化为JSON并返回一个适当的响应(正确设置所有头文件以及所有这些)。通常JSON请求需要POST,但您可以将其配置为允许GET。

#1


6  

Are you looking for number 4 or 6 bellow?

你在找4号或6号低于标准杆?

Text extracted from here:

从这里提取的文字:

Understanding Action Results

了解行动结果

A controller action returns something called an action result. An action result is what a controller action returns in response to a browser request.

控制器操作返回称为操作结果的内容。操作结果是控制器操作响应浏览器请求而返回的内容。

The ASP.NET MVC framework supports several types of action results including:

ASP.NET MVC框架支持几种类型的操作结果,包括:

  1. ViewResult - Represents HTML and markup.
  2. ViewResult - 表示HTML和标记。
  3. EmptyResult - Represents no result.
  4. EmptyResult - 表示无结果。
  5. RedirectResult - Represents a redirection to a new URL.
  6. RedirectResult - 表示重定向到新URL。
  7. JsonResult - Represents a JavaScript Object Notation result that can be used in an AJAX application.
  8. JsonResult - 表示可以在AJAX应用程序中使用的JavaScript Object Notation结果。
  9. JavaScriptResult - Represents a JavaScript script.
  10. JavaScriptResult - 表示JavaScript脚本。
  11. ContentResult - Represents a text result.
  12. ContentResult - 表示文本结果。
  13. FileContentResult - Represents a downloadable file (with the binary content).
  14. FileContentResult - 表示可下载的文件(带有二进制内容)。
  15. FilePathResult - Represents a downloadable file (with a path).
  16. FilePathResult - 表示可下载文件(带路径)。
  17. FileStreamResult - Represents a downloadable file (with a file stream).
  18. FileStreamResult - 表示可下载文件(带有文件流)。

All of these action results inherit from the base ActionResult class.

所有这些操作结果都继承自ActionResult基类。

#2


1  

Return it as a ContentResult rather than an ActionResult

将其作为ContentResult而不是ActionResult返回

I use something like

我喜欢用的东西

    public ContentResult Place(string person, string seat)
    {
        string jsonString = null;
        try
        {

            jsonString = AllocationLogic.PerformAllocation(person, seat);
        }
        catch {
            jsonString = AllocationLogic.RaiseError(timeout);
        }
        return Content(jsonString);
    }

#3


1  

Are you sure JsonResult isn't what you want? If you call the Json(object jsonObject) method that is defined in Controller, it will serialize jsonObject into JSON and return an appropriate response (with all the headers correctly set and all that). Generally JSON requests need to be POST, but you can configure it to allow GET too.

你确定JsonResult不是你想要的吗?如果调用Controller中定义的Json(对象jsonObject)方法,它会将jsonObject序列化为JSON并返回一个适当的响应(正确设置所有头文件以及所有这些)。通常JSON请求需要POST,但您可以将其配置为允许GET。