简体   繁体   English

如何动态创建css / js / html文件并使用ASP.NET MVC在运行时提供服务

[英]How to dynamically create css/js/html files and serve back at runtime with ASP.NET MVC

Ok, perfect examples: tryIt Editor (w3schools), jsbin, jsfiddle. 好的,完美的例子:tryIt编辑器(w3schools),jsbin,jsfiddle。 I want a user to type css/js/html into different textareas, hit a magic button and poof, the output's displayed in an iframe. 我希望用户在不同的textareas中键入css / js / html,按一下魔术按钮并修饰,输出显示在iframe中。

Code examples are appreciated but I'm more after answers to how to go about it than exact implementation. 代码示例是值得赞赏的,但是我比真正的实现更渴望获得解决方案。 For example, I can easily send the js / html / css as strings to the server. 例如,我可以轻松地将js / html / css作为字符串发送到服务器。 Then dynamically create files for them. 然后为它们动态创建文件。 But then what? 但是那又怎样呢?

I want to have these files exist merely for the POST & GET. 我想让这些文件仅存在于POST&GET中。 I dont want to remake a jsfiddle, I want to quickly show a user what output they have, not save it for later. 我不想重新制作jsfiddle,我想快速向用户显示他们有什么输出,而不是将其保存以供以后使用。 So I dont want these files saved to memory. 所以我不希望这些文件保存到内存中。 I want them sent to the user, and if they refresh the page then it's gone. 我希望将它们发送给用户,如果他们刷新页面,则该页面消失了。

  1. Is the idea of creating files (and removing the old ones each update) a good idea? 创建文件(并在每次更新时删除旧文件)的想法是一个好主意吗?
  2. Should it all just be done client-side in javascript? 是否应该全部使用JavaScript在客户端完成?
  3. If using files is the correct method, how can I serve them up? 如果使用文件是正确的方法,我该如何提供文件? I'm thinking create a file to a temp folder on the server, then POST the link, then send a DELETE request after 300ms with a unique ID + salt, which deletes the temp file if it still exists. 我正在考虑在服务器上的临时文件夹中创建文件,然后发布链接,然后在300毫秒后发送具有唯一ID + salt的DELETE请求,如果该临时文件仍然存在,则会删除该临时文件。 But this is far from ideal I can think of a few issues immediately 但这远非理想,我可以立即想到一些问题

Any server-side action method can technically return any kind of response. 从技术上讲,任何服务器端操作方法都可以返回任何类型的响应。 So instead of thinking about files, think about response types. 因此,与其考虑文件,不如考虑响应类型。 Namely: 即:

  • text/html text / html的
  • text/css 文/ CSS
  • application/javascript (or maybe text/javascript?) 应用程序/ javascript(或者文本/ javascript?)

So you'd need at least three action methods. 因此,您至少需要三种操作方法。 Pressing the "magic button" can, for example, reload a frame (with a POST request which includes the HTML/CSS/JS inputs) which: 例如,按下“魔术按钮”可以重新加载框架(带有包含HTML / CSS / JS输入的POST请求):

  • invokes the action method which returns text/html , which itself: 调用操作方法,该方法返回text/html ,它本身:
  • includes standard tags in the head node which reference the routes for the CSS and JavaScript "files" (which aren't really files, just action responses) head节点中包含标准标记,这些标记引用CSS和JavaScript“文件”的路径(这些文件不是真正的文件,只是动作响应)

One handy way to return custom responses from the action methods would be with the ContentResult type: 从操作方法返回自定义响应的一种简便方法是使用ContentResult类型:

return Content(htmlText, "text/html");

Or: 要么:

return Content(cssText, "text/css");

So your server-side code can, for example, take the CSS text from the client-side and just echo it back like that. 因此,您的服务器端代码可以例如从客户端获取CSS文本,然后像这样将其回显。 (Beware of various attacks, such as XSS. This simplistic implementation isn't likely ideal, but should get you started.) The only one you'd really need to wrap in any custom text would be the HTML response, where you can just statically define the HTML server-side and put the user-input HTML in just the body element. (请注意各种攻击,例如XSS。这种简单的实现可能不太理想,但是应该让您入门。)真正需要包装在任何自定义文本中的唯一方法就是HTML响应,您可以在其中进行静态定义HTML服务器端,并将用户输入的HTML放在body元素中。

So just thinking out loud, if the POST request for the frame/iframe consists of the three values, then the basic HTML server-side action might look like: 因此,请大声考虑一下,如果对frame / iframe的POST请求包含三个值,那么基本的HTML服务器端操作可能类似于:

public GetHTML(string html, string css, string javascript)
{
    // temporarily store the css somewhere server-side
    // temporarily store the javascript somewhere server-side
    // build the html response from static dressing around the user-input html
    return Content(htmlString, "text/html");
}

The CSS/JS are stored in a temporary server-side location, possibly utilizing session state or any built-in temporary storage mechanism. CSS / JS存储在服务器的临时位置,可能利用会话状态或任何内置的临时存储机制。 After the above action returns its response, the browser will request the CSS/JS actions from the tags in that response, those are simple enough: 在上述操作返回其响应之后,浏览器将从该响应中的标签请求CSS / JS操作,这些操作很简单:

public GetCSS()
{
    // if the temporary location (session?) has a css value
    //   get that value
    // else
    //   get a default, likely empty value
    return Content(cssString, "text/css");
}

// repeat for the javascript content

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM