Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to return a FileResult from a string in asp.net mvc

Tags:

I need to write an action that will return a FileResult from a string

like image 691
Omu Avatar asked Dec 17 '09 10:12

Omu


People also ask

Can we return FileResult from API?

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 .

What is ActionResult return type in MVC?

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.

What is FileContentResult in C#?

FileContentResult(Byte[], String) Initializes a new instance of the FileContentResult class by using the specified file contents and content type.


1 Answers

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; 
like image 98
Klaus Byskov Pedersen Avatar answered Sep 29 '22 16:09

Klaus Byskov Pedersen