I need to write an action that will return a FileResult from a string
In ASP.NET Core, a Web API action method usually returns an ActionResult object. When we want to return a file response, we can explicitly set the return type for the action method to be FileResult , which is a type inherited from ActionResult .
An ActionResult is a return type of a controller method in MVC. Action methods help us to return models to views, file streams, and also redirect to another controller's Action method.
FileContentResult(Byte[], String) Initializes a new instance of the FileContentResult class by using the specified file contents and content type.
You can use the FileContentResult
class.
var contentType = "text/xml"; var content = "<content>Your content</content>"; var bytes = Encoding.UTF8.GetBytes(content); var result = new FileContentResult(bytes, contentType); result.FileDownloadName = "myfile.xml"; return result;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With