How to Serve HTML File from Another Directory as Actionresult

How to serve html file from another directory as ActionResult

If you want to render this index.htm file in the browser then you could create controller action like this:

public void GetHtml()
{
var encoding = new System.Text.UTF8Encoding();
var htm = System.IO.File.ReadAllText(Server.MapPath("/Solution/Html/") + "index.htm", encoding);
byte[] data = encoding.GetBytes(htm);
Response.OutputStream.Write(data, 0, data.Length);
Response.OutputStream.Flush();
}

or just by:

public ActionResult GetHtml()
{
return File(Server.MapPath("/Solution/Html/") + "index.htm", "text/html");
}

So lets say this action is in Home controller and some user hits http://yoursite.com/Home/GetHtml then index.htm will be rendered.

EDIT: 2 other methods

If you want to see raw html of index.htm in the browser:

public ActionResult GetHtml()
{
Response.AddHeader("Content-Disposition", new System.Net.Mime.ContentDisposition { Inline = true, FileName = "index.htm"}.ToString());
return File(Server.MapPath("/Solution/Html/") + "index.htm", "text/plain");
}

If you just want to download file:

public FilePathResult GetHtml()
{
return File(Server.MapPath("/Solution/Html/") + "index.htm", "text/html", "index.htm");
}

How do you request static .html files under the ~/Views folder in ASP.NET MVC?

I want to be able to request static .html files which are located in the '~/Views' folder.

You can't. There's a web.config file in this folder which explicitly forbids accessing any file from it. If you want to be able to access files from the client those files should not be placed in the Views folder which has a special meaning in ASP.NET MVC.

You could have a ~/Static folder where you could place your HTML files. And then access it like that:

http://example.com/yourapplicationname/static/foo.html

How to open html file that links with images and stuffs in mvc

If i understand what you are trying to do here, you should not return an HTML file in the controller action but rather generate the view of the index page. If you are new to MVC you might benefit from reading more about Model-View-Controller pattern and when to apply it.

If all you are trying to do is serve a static HTML file, just let IIS serve that file and point the browser at the URL of that file directly. (i.e. Where it is stored on your web site).

If you want to have some logic dynamically linking to some static file, there are many ways to do it (both on the server and on the client), one of them would be to calculate the correct URL and redirect the user by returning a Redirect() operation from your controller action.

Recommended way to create an ActionResult with a file extension

I think your Response MUST contain "Content-Disposition" header in this case. Create custom ActionResult like this:

public class MyCsvResult : ActionResult {

public string Content {
get;
set;
}

public Encoding ContentEncoding {
get;
set;
}

public string Name {
get;
set;
}

public override void ExecuteResult(ControllerContext context) {
if (context == null) {
throw new ArgumentNullException("context");
}

HttpResponseBase response = context.HttpContext.Response;

response.ContentType = "text/csv";

if (ContentEncoding != null) {
response.ContentEncoding = ContentEncoding;
}

var fileName = "file.csv";

if(!String.IsNullOrEmpty(Name)) {
fileName = Name.Contains('.') ? Name : Name + ".csv";
}

response.AddHeader("Content-Disposition",
String.Format("attachment; filename={0}", fileName));

if (Content != null) {
response.Write(Content);
}
}
}

And use it in your Action instead of ContentResult:

return new MyCsvResult {
Content = Emails.Aggregate((a,b) => a + Environment.NewLine + b)
/* Optional
* , ContentEncoding = ""
* , Name = "DoNotEmailList.csv"
*/
};

How to load local html page in web form

There are many ways to do it but I would like to mention the 2 which worked for me

In this approach the response will be redirected to the page you are passing.

Response.Redirect("~/Results/xyz.html", false);
HttpContext.Current.ApplicationInstance.CompleteRequest();

In this below mentioned approach the content of the html page which you wish to render will be read and then passed on using OutputStream.

var encoding = new System.Text.UTF8Encoding();
var htm = System.IO.File.ReadAllText(Server.MapPath("/Results/Html/") + "xyz.html", encoding);
byte[] data = encoding.GetBytes(htm);
Response.OutputStream.Write(data, 0, data.Length);
Response.OutputStream.Flush();

Thanks to everyone who has contributed here!

Get files from folder based on conditions mvc asp

In your new code:

files[counter] = Directory.GetFiles(Server.MapPath("/Files/"+item.FileName))

You are using an incorrect parameter. You are passing a file path, but this method needs a directory path.

You can use this:

var filteredByFilename = Directory
.GetFiles(Server.MapPath("/Files"))
.Select(f => Path.GetFileName(f))
.Where(f => f.StartsWith("yourFilename"));

ViewBag.Files = filteredByFilename.ToArray();

But in your case I think is better to use Directory.EnumerateFiles method. Please read here.

So the new code would be:

var filteredByFilename = Directory
.EnumerateFiles(Server.MapPath("/Files"))
.Select(f => Path.GetFileName(f))
.Where(f => f.StartsWith("yourFilename"));

ViewBag.Files = filteredByFilename.ToArray();

The whole Download method could be:

public ActionResult Download(int NoteID)
{
var fs = db.Files.Where(f => f.NoteID == NoteID);

var fileNames = Directory
.EnumerateFiles(Server.MapPath("/Files"))
.Select(f => Path.GetFileName(f));

var result = new List<string>();

foreach (var fileName in fs)
{
var filteredByName = fileNames.Where(f => f.StartsWith(fileName));
result.AddRange(filteredByName);
}

ViewBag.Files = result.ToArray();
return View();
}

Render HTML file in ASP.NET MVC view?

You can't use Html.Partial for this.It is a special helper method for rendering Partial Views. Instead you can add an Action like this:

[ChildActionOnly]
public ActionResult GetHtmlPage(string path)
{
return new FilePathResult(path, "text/html");
}

And you can call it from your View with using Html.Action helper Method:

@Html.Action("GetHtmlPage","controllername", new { path = "~/Test/main.html" })


Related Topics



Leave a reply



Submit