1

I have many static HTML files (Lets say 1.html to 100.html). Is there any way that I can create a link like Files/get/1 (where "Files" is the controller and "get" is the action). Read the file based on the passed id and put the file's content inside my site layout and send it to user.

In this way the format of those Html file will be preserved, and I wouldn't need to create a View for each file.

I am new to MVC and will appreciate any suggestion/hint. Let me know if the question is not clear.

Thanks for the help in advance. Reza,

Edit: Added what I ended up doing.

Answer: So I used what Darin said below, combined it with a little bit of jQuery and got exactly what I needed. Which was loading static HTML files inside my layout. Here is the sample of what I did:

First I created two methods in my Controller:

    public ActionResult GetHelp(String id)
    {
        var folder = Server.MapPath(Config.get().Help_Folder);
        var file = Path.Combine(folder, id + ".html");
        if (!System.IO.File.Exists(file))
        {
            return HttpNotFound();
        }
        return Content(System.IO.File.ReadAllText(file), "text/html");

    }


    public ActionResult GetHelper(String id)
    {

        ViewBag.helpPath = id; 
        return View();

    }

Then I created a view called GetHelper, which uses my layout, and added the below code to it:

<script type="text/javascript">

$(function () {
    var path = "@ViewBag.helpPath"
    path = "@Url.Content("~/Helps/GetHelp/")" + path;
    $('#help-content').load(path);

});
</script>

<div id="help-content">

</div>

And it works perfectly. The only downside is for each page we get two server requests :(

1 Answer 1

2

Something among the lines:

public class FileController : Controller
{
    public ActionResult Index(string id)
    {
        var folder = Server.MapPath("~/SomePathForTheFiles");
        var file = Path.Combine(folder, id + ".html");
        if (!System.IO.File.Exists(file))
        {
            return HttpNotFound();
        }
        return Content(System.IO.File.ReadAllText(file), "text/html");
    }
}

and if you wanted the user do download those files:

return File(file, "text/html", Path.GetFileName(file));

and because those are static files you could cache them by decorating your controller action with the [OutputCache] attribute:

[OutputCache(Location = OutputCacheLocation.Downstream, Duration = 10000, VaryByParam = "id")]
public ActionResult Index(string id)
Sign up to request clarification or add additional context in comments.

3 Comments

Another question. Is there any way that I can add my layout to it?
@Reza, you want to apply your dynamic _Layout.cshtml to your static HTML files? Are those HTML files entire HTML pages or only HTML fragments in this case? Where exactly do you want this HTML fragment to be rendered in the layout? At the @RenderBody() location?
Exactly, what I needed is to put them into @RenderBody() location. I came up with a solution using your answer and a little bit of JQuery. Will post it shortly, but if I can find a solution that let me put file content directly into the @RenderBody()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.