Generate Static HTML Pages to replace dynamic pages - asp.net

For one project, we've to generate static .html pages, which are gonna to be published on the same server to serve to millions of visitors.
We've to automate the creation of those files from a c# code, which takes data from a SQL Server database.
The project is already developed using C# asp.net MVC3, and we need to store the dynamically generated pages in .html on the same url to be served to visitors.
I was wondering how to use asp.net MVC3/Razor to generate those .html pages?
I don't want/need to use web caching, for a lot of reasons(load(millions of pages loaded every day), these static pages will be cached on CDN network to further serve super fast without original server going into picture, number of pages are really too many (caching will only help me if I've the same pages a lot of time, but I will have more than million pages visited very frequently, so I will have to generate them often.)
So I really search something to generate HTML pages.
Any idea how to do this...

To start with, make sure your routes all produce urls that can be duplicated as static html files. So that your calls to Html.ActionLink will produce urls you can use.
Generate your whole site as if you are using it directly, and then let it be cached externally.
You could use something like wget on Linux to grab the whole html tree of the site, and put those up along with the content files; css, images, javascript, etc.
Then redownload the site when there are changes.

In my company we've done something similar. We have a separate program that goes trhough a list of urls, sends a http-request against them. Saves the result and copies it out to the web servers. This way we only have one web server with asp-code on it internally on the network and the servers on the internet has static copies of the dynamic pages. And we get some great performance out of it.
In order for you to get the list of urls you would probably have to create a special view/controller that queries the database for the keys that can be used to query the info you want. So if you have for example a site that shows hamburgers, your list view that creates the urls might query your burger-table and create a bunch of /myburger?name=Wopper type urls. Then your batch-program reads those urls and as described before, does a http-request and saves the result etc.

If you want to generate html based on the mvc views and models, you could use the Razor.
I have used it to generate email templates, where we have used the Razor to inject the model into a view. You could generate the html from the views and write them into static html files if that fits your purpose.
Refer Razor Engine from NuGet, And you could use it like
var html = Razor.Parse(templateView, model);
If you want more customization on it, May be this tutorial could help you.
http://www.west-wind.com/weblog/posts/2012/May/30/Rendering-ASPNET-MVC-Views-to-String

I always use my own email generating method instead of MvcMailer.
First, you should generate a string from your view or partial, then add/remove some html tags, such as <html> etc, if you need... Next write this string into a file, save it as a .html file to your path.
public static string HTMLToString(ControllerContext context, string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = context.RouteData.GetRequiredString("action");
var viewData = new ViewDataDictionary(model);
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(context, viewName);
var viewContext = new ViewContext(context, viewResult.View, viewData, new TempDataDictionary(), sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}

Related

ASP.Net Core static file security (images specifically)

I have an issue that seems like a very common requirement, but I'm unable to find any help. Let's say I have an authenticated user uploading private photos to non browsable folder on my server. Each user has their own folder in a large file store, like...
/FileStore/{UserId}/Photos/my_cute_cat.jpg
The file is uploaded and I save a thumbnail of the photo like...
/FileStore/{UserId}/Photos/Thumbs/my_cute_cat_thumb.jpg
That user wants to download their photo. No problem...
User sends a request to download
I authorize the user and make sure they own that particular photo
I serve the file
I need display the thumbnail in a plain old img tag on the user's dashboard. The /Thumbs/ folder is not set up to serve static images. I don't want the /thumbs/ folder to be able to serve static images because they should only be visible to authorized users. What am I supposed to do?
If its just a small thumb nail, consider using embedded base64 image with more details here:
How to display Base64 images in HTML?
You can pass base64 down to the View by encoding the file into a base 64 format as a string explained here:
http://www.devcurry.com/2009/01/convert-string-to-base64-and-base64-to.html
Using this approach or even using a FileActionResult to serve the file through a controller has the big disadvantage of not being able to use a CDN to deliver the cached content. What you can do to help with this is still serve the images statically but give them obscenely long random names which is unguessable. When someone requests the image from you, then you simply provide them with the unguessable url.
First and foremost, if a static file should not be available to the whole world, then your web server should not serve the directory it is in at all. Nothing else will do on that front. If the directory is served, then the image can leak.
That then presents the problem of how to allow the user to access it. The simple answer there is that you need an authorized action that returns the image(s). Basically, that's going to look something like:
[Authorize]
public async IActionResult Image(string image)
{
var file = $"/FileStore/{User.Identity.GetUserId()}/Photos/{image}";
if (!File.Exists(file))
return NotFound();
return File(file);
}
Now, obviously, I don't know about your "FileStore", so the exact code here may need to change. The general idea is that you simply see if this file exists under the user's directory. If it does, they can have it. If not, they don't own it. You also should probably add in a bit more security, such as restricting the image param to only image types, so someone can't try to pull just any arbitrary file. They'd still have to somehow manage to get some aberrant file there, in the first place, but just in case it's better to not take chances.
The return methodology may need to change as well. Right now, I'm assuming a file on the filesystem of the server, so I just return a FileResult with the path. If you're pulling it from some sort of storage account in Azure, AWS, etc. then you'd use HttpClient to make the request, and then you should stream the response from that directly to a FileStreamResult.
I've not tested on linux, but if you make a folder where you have you pictures, you can actually just create a controller method that returns a file. Now, even if you are using a view served from another method, you can still call this file method from this razor view and display the image.
in controller called App I serve the image from a folder called Documents:
public IActionResult File(string id)
{
if (String.IsNullOrEmpty(id))
{
return PhysicalFile(Path.Combine(_env.ContentRootPath, "Documents", "Wrong.png"), "image/jpg");
}
return PhysicalFile(Path.Combine(_env.ContentRootPath, "Documents", id), "image/jpg");
}
in razor (using a bit bootstrap):
<img src="~/App/File/#profilePicture" class="img-fluid" />
use the location of your method that serves the file.

asp.net mvc3 - external includes or sharing layouts across projects?

Ok, I'm still new to Asp.Net and MVC3. I'm becoming more familiar with things but still experimenting after finishing my first web application (a simple web form submission into a database). Now I am working on smaller projects such as converting some old ColdFusion web forms that submit e-mails. I've easily accomplished this in MVC3 but all of our web pages are in a separate content management system where our central HTML template is. I've already asked a question about this here and didn't get anywhere.
What we have is the majority of our web pages get exported from the CMS as straight HTML files, and only the ones that need database access or a programming language are exported as ColdFusion. It's very easy to "include" ColdFusion code to use inside of the template in our CMS. I would love to be able to use this HTML template in my mvc3 project but I've found no way to perform an "include" or link to an external file. I'm not sure how this would work anyway, so I settled on just copy/pasting the template to mvc3 and figuring out a way I can share this template (now a "layout") between all of the small projects I'll be working on. If the template changes I do not want to have to update every single little mvc3 web application. I learned about using "Areas" but it seems you can't just publish a single area to a folder on the web server, the whole project has to be deployed.
All I really need is a way for small mvc3 projects to use one template and these small mvc3 projects to be scattered all over our web server. Would this best be done in one large project that publishes to multiple different folders, or as many small projects that can share a common layout? Is either of these two possible?
After attempting and experimenting with all of this, I'm beginning to think MVC is not going to work with what I want. It seems better suited for intranet applications or entire web sites, not this little "here and there" applications like what I want. Should I learn Web Forms instead? I know I can "include" a aspx file inside our CMS much like I do with ColdFusion.
Do your templates have to be "exported" from the CMS? Or, can you have a template that "lives" on a static CMS URL? This is what we do for apps that need db access / can't be easily done within the CMS, but need to share the same look and feel.
What you can do is have your plain old HTML file live at a URL, for example, https://cms.domain.tld/templates/designxyz.html. That file will serve up a basic layout, except where your custom app content goes, you simply have the string "content goes here".
Then, from the MVC app, you can call this URL to get the HTML content as a string. Once you have the string, you can split it in 2 before and after the "content goes here" string. Then, in your layout.cshtml file, you can do something like this:
#{
const string contentPlaceholder = "content goes here";
var allHtml = GetHtmlTemplateFromLiveServer();
var index = allHtml.IndexOf(contentPlaceholder);
var topHtml = allHtml.Substring(0, index);
var botHtml = allHtml.Substring(index + contentPlaceHolder.Length);
}
#topHtml
#RenderBody()
#botHtml
If something like this works, you can then abstract all of this away into a HTML Helper, then reuse that helper in other projects (NuGet would be good for this).
_Layout.cshtml
#{
var options = new CmsTemplateRenderOptions
{
Url = "https://cms.domain.tld/templates/designxyz.html",
Cache = new TimeSpan(1, 0, 0);
};
}
#Html.RenderCmsTemplate(CmsTemplateRenderRegion.Top, options)
#RenderBody()
#Html.RenderCmsTemplate(CmsTemplateRenderRegion.Bottom, options)
Then, to update the layout for all of your apps, you would just publish changes to the https://cms.domain.tld/templates/designxyz.html URL.

asp.net mvc how to manage urls/links and routing centrally (c# + js)

I keep running into problems with URLs and routing.
Couldn't find an answer on SO.
I would like to manage all of my urls/links in a single place.
This is for my C# MVC code and the js/jquery ajax code.
These urls are scattered throughout my application.
Moving to a production server needs some fixes and I don't like the fact that I need to look for all of the occurrences in the application.
I don't mind fixing this once - but I would like to do it only once.
Any ideas how to manage all of these links/urls as a group will be very appreciated.
Be happy ad enjoy life, Julian
Consider using T4MVC
You could use Html.ActionLink or
Html.BuildUrlFromExpression(c => c.ControllerAction())
Depends, if you have application reading off certain urls and those urls changed once in a while. then you might want to consider putting all those urls into a database table/etc and retrieve them using specific key.
that way, when your url changed, all you need to do is to change the url on your database and all your application will still be running fine.
Urls should be managed in a single place: the RegisterRoutes static method in Global.asax. In absolutely every other part of your application you should use Html helpers when dealing/generating urls. This way you will never have problems because helpers take into account your routing system.
So instead of writing:
$('#foo').click(function() {
$('#result').load('/mycontroller/myaction');
return false;
});
you use an HTML helper to generate this foo:
<%: Html.Action("foo", "myaction", "mycontroller") %>
and then:
$('#foo').click(function() {
$('#result').load(this.href);
return false;
});
Never hardcode a single url in your application except of course in global.asax which is the only centralized place urls should be defined. So basically every time you find yourself writing something of the form /foo/bar in some other part than global.asax you are doing it wrong.

What is the best practice for using ASP.NET MVC to render lots of html or text files?

I have a lot of html pages, but I don't know how to display them through the asp.net mvc view.
I buid a view as my template and use asp.net mvc to insert html into the template and then render it.
But the question is that I must use FileStream to read the raw html-based files into memroy and then put it into view template, like ViewData["content"] = ???.
I just want to know if there are some other better ways to render static html files to the browser.
Did i describe the question clearly?
I guess you could do something like this:
using(var file = new StreamReader(htmlFileName))
{
return Content(file.ReadToEnd());
}
Note that the mime type automatically defaults to text/html, but you could optionally specify which mime type headers should be sent by supplying the type as an additional argument to the Content method.
I guess you also can point a iframe element from HTML to the target file url directly.
Alternatively you could write your own ActionResult that writes the contents of the file to Response.Output (could potentially avoid loading the entire file into memory at once albeit it might not be a big issue).

Export ASPX to HTML

We're building a CMS. The site will be built and managed by the users in aspx pages, but we would like to create a static site of HTML's.
The way we're doing it now is with code I found here that overloads the Render method in the Aspx Page and writes the HTML string to a file. This works fine for a single page, but the thing with our CMS is that we want to automatically create a few HTML pages for a site right from the start, even before the creator has edited anything in the system.
Does anyone know of any way to do this?
I seem to have found the solution for my problemby using the Server.Ecxcute method.
I found an article that demonstared the use of it:
TextWriter textWriter = new StringWriter();
Server.Execute("myOtherPage.aspx", textWriter);
Then I do a few maniulatons on the textWriter, and insert it into an html file. Et voila! It works!
Calling the Render method is still pretty simple. Just create an instance of your page, create a stub WebContext along with the WebRequest object, and call the Render method of the page. You are then free to do whatever you want with the results.
Alternatively, write a little curl or wget script to download and store whichever pages you want to make static.
You could use wget (a command line tool) to recursively query each page and save them to html files. It would update all necessary links in the resulting html to reference .html files instead of .aspx. This way, you can code all your site as if you were using server-generated pages (easier to test), and then convert it to static pages.
If you need static HTML for performance reasons only, my preference would be to use ASP.Net output caching.
I recommend you do this a very simple way and don't do it in code. It will allow your CMS code to do what the CMS code should do and will keep it as simple as possible.
Use a product such as HTTrack. It calls itself a "website copier". It crawls a site and creates html output. It is fast and free. You can just have it run at whatever frequency you think is best.
It decouples your HTML output needs from your CMS design and implementation. It reduces complexity and gives you some flexibility in how you output the HTML without introducing failure points in your CMS code.
#ckarras: I would rather not use an external tool, because I want the HTML pages to be created programmatically and not manually.
#jttraino: I don't have a time interval in which the site needs to be outputted- the uotput has to occur when a user creates a new site.
#Frank Krueger: I don't really understand how to create an instance of my page using WebContext and WebRequest.
I searched for "wget" in searchdotnet, and got to a post about a .net class called WebClient. It seems to do what I want if I use the DownloadString() method - gets a string from a specific url. The problem is that because our CMS needs to be logged in to, when the method tries to reach the page it's thrown to the login page, and therefore returns the login.aspx HTML...
Any thoughts as to how I can continue from here?

Resources