使用.net WebApi的HTML页面和图像 [英] HTML page with images using .net WebApi
问题描述
我正在使用.net WebApi来使用JSON数据公开不同的终结点.现在,我需要特定的端点返回HTML页面.我是这样实现的.
I'm using .net WebApi to expose different endpoints with JSON data. Now I need that an specific endpoint returns HTMLs pages. I have implemented it like this.
[HttpGet]
[Route("htmlPage/{htmlPageId}")]
public HttpResponseMessage GetHtmlPage(string htmlPageId) {
string rawHtml = m_logic.GetHtmlPageById(htmlPageId);
HttpResponseMessage response = new HttpResponseMessage {Content = new StringContent(rawHtml)};
response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
return response;
}
我面临的问题是该HTML页面上有一些图像.我不知道如何公开它们,因为当我从浏览器中调用端点时,出现了404 not found错误.我的项目中有一个包含这些图像和一些我必须返回的Javascript脚本的文件夹.
The problem that I'm facing is when that HTML page have some images. I don't know how to expose them because now I'm getting a 404 not found error when I call my endpoint from my browser. I have a folder in my project with these images and some Javascript scripts that I must return.
我该怎么做?
这是我的HTML的一部分
This is part of my HTML
<html lang=\"en\">
<head>
<title> Simplest HTML </title>
<meta name=\"description\" content=\"The Html served\">
<meta name=\"author\" content=\"acostela\">
<script type="text/javascript" src="./public/scripts/my-api.js"></script>
</head>
<body>
<h1> The first Simplest html served!!</h1>
<img src="./public/img/success.jpg"/></br></br>
</body>
</html>
我的文件夹结构如下
-->Html
|
|
---> myFirstHtml.html
|
|
---> public
|
|
---> img
| |
| |
| ---> success.jpg
|
|
---> scripts
|
|
---> my-api.js
推荐答案
最后,我解决了它.我使用owin + katana添加了静态文件服务器中间件.
Finally I solved it. I added a static file server middleware using owin+katana.
我使用此更改了我的启动课程
I changed my startup class using this
public void Configuration(IAppBuilder application)
{
//Other webApi configuration
application.UseFileServer(new FileServerOptions()
{
RequestPath = new PathString("/html/public"),
FileSystem = new PhysicalFileSystem(@"../../../MyProject/html/public"),
});
现在,当通过webApi请求我的html页面时,将自动提供所有公开的图像脚本和文件.
Now automatically all images scripts and files that are under public are served when my html page is requested via my webApi.
此代码的作用是将我的http请求"/html/public"转换为PhysicalFileSystem中指定的真实文件系统路径.
What does this code is to translate my http requests "/html/public" to my real file system path specified in PhysicalFileSystem.
希望对大家有帮助.
这篇关于使用.net WebApi的HTML页面和图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!