时间:2019-03-17 标签:c#AzureFunctions分离html和css

标签 c# azure azure-functions

我目前正在使用 Azure Functions 为内部系统生成 html 页面;所以它不需要托管在网络服务器上来满足我的需要。 Iv 目前正在将 html 复制并粘贴到字符串变量中并将其作为响应返回。然而,这意味着我必须将 CSS 和 html 组合成一个大的长字符串,并且很难组织和管理。

有没有办法将 html 和 css 分成单独的文件并将其链接到 azure 函数作为响应?

结构示例:

  • \View\HomePage\HomePage.html、HomePageAzureFunction.cs
  • \View\Style\Style.css
  • \View\Javascript\Javascript.js

最佳答案

Is there way of separating the html and css into separate files and linking it to azure function as response?

您可以利用kudu或 ftp 工具访问您的 azure 函数文件,然后添加相关目录和文件(html、css 等),如下所示:

enter image description here

run.csx:

#r "System.Web"

using System.Web;
using System.Net;
using System.Net.Http.Headers;

public static HttpResponseMessage Run(HttpRequestMessage req,TraceWriter log)
{ 
    var response = req.CreateResponse(HttpStatusCode.OK);
    var stream = new FileStream(@"d:\home\site\wwwroot\HttpTriggerCSharp4\view\Home\index.html", FileMode.Open);
    response.Content = new StreamContent(stream);
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
    return response;
}

注意: css 和 javascript 文件无法正确加载,如下所示:

https://<your-functionapp-name>.azurewebsites.net/<your-HttpTriggerName>/view/style/site.css

对于这种情况,您可能需要添加其他 HttpTrigger 端点来访问 css 和 javascript 文件。对我来说,我会使用 Azure Blob 存储来存储 CSS 和 javascript 文件,详细信息您可以关注 here 。您可以在 html 文件中添加 css 和 javascript 文件,如下所示:

<link rel="stylesheet" href="https://{your-storage-account}.blob.core.windows.net/{container-name}/view/style/site.css">

<script src="https://{your-storage-account}.blob.core.windows.net/{container-name}/view/javascript/site.js"></script>

关于时间:2019-03-17 标签:c#AzureFunctions分离html和css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49375209/

相关文章:

azure - Windows Azure App Fabric 缓存整个 Azure 数据库表

C# 解决方案 : using one "Globals" project for external dll's?

c# - 在 ASP.Net、C# 中以编程方式创建字段集、ol/ul 和 li 标记

c# - 带有最小起订量的 Web.conf 中的模拟设置

reactjs - 如何从 React Web 应用程序将文件上传到 azure blob?

c# - 自定义角色存储 asp.net

c# - 从 Azure 函数调用 Asp.Net Web API 端点

azure - 从二头肌模块检索存储帐户访问 key

azure - 在 Azure Function 中运行时未找到 HttpRequestMessageExtensions

c# - asp.net mvc如何汇总数据库的一列并通过标签显示?