Skip to content

Commit 4d329d5

Browse files
committed
#2: fix .net core example
1 parent 1970265 commit 4d329d5

File tree

4 files changed

+29
-27
lines changed

4 files changed

+29
-27
lines changed

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,25 @@ Or Standalone this:
3030

3131
```csharp
3232

33-
[HttpGet]
34-
public IActionResult Get()
35-
{
36-
var registry = CollectorRegistry.Instance;
37-
var acceptHeaders = Request.Headers["Accept"];
38-
var contentType = ScrapeHandler.GetContentType(acceptHeaders);
39-
Response.ContentType = contentType;
40-
string content;
41-
42-
using (var outputStream = new MemoryStream())
33+
[Route("[controller]")]
34+
public class MetricsController : Controller
4335
{
44-
var collected = registry.CollectAll();
45-
ScrapeHandler.ProcessScrapeRequest(collected, contentType, outputStream);
46-
content = Encoding.UTF8.GetString(outputStream.ToArray());
36+
[HttpGet]
37+
public void Get()
38+
{
39+
var registry = CollectorRegistry.Instance;
40+
var acceptHeaders = Request.Headers["Accept"];
41+
var contentType = ScrapeHandler.GetContentType(acceptHeaders);
42+
Response.ContentType = contentType;
43+
Response.StatusCode = 200;
44+
using (var outputStream = Response.Body)
45+
{
46+
var collected = registry.CollectAll();
47+
ScrapeHandler.ProcessScrapeRequest(collected, contentType, outputStream);
48+
}
49+
}
4750
}
4851

49-
return Ok(content);
50-
}
51-
5252
```
5353

5454

examples/WebApiApplication/Controllers/MetricsController.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.IO;
22
using System.Net;
33
using System.Net.Http;
4+
using System.Net.Http.Headers;
45
using System.Net.Mime;
56
using System.Text;
67
using System.Web.Http;
@@ -19,17 +20,22 @@ public HttpResponseMessage Get()
1920

2021
var contentType = ScrapeHandler.GetContentType(acceptHeaders);
2122

22-
var response = Request.CreateResponse(HttpStatusCode.OK);
23+
2324
string content;
2425

2526
using (var outputStream = new MemoryStream())
2627
{
2728
var collected = registry.CollectAll();
2829
ScrapeHandler.ProcessScrapeRequest(collected, contentType, outputStream);
29-
content = Encoding.UTF8.GetString(outputStream.ToArray());
30+
content = Encoding.ASCII.GetString(outputStream.ToArray());
3031
}
3132

32-
response.Content = new StringContent(content, Encoding.UTF8, new ContentType(contentType).MediaType);
33+
34+
var response = Request.CreateResponse(HttpStatusCode.OK);
35+
36+
response.Content = new StringContent(content, Encoding.UTF8, contentType);
37+
38+
3339
return response;
3440

3541
}

examples/WebCoreApplication/Controllers/MetricsController.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,18 @@ namespace WebCoreApplication.Controllers
1111
public class MetricsController : Controller
1212
{
1313
[HttpGet]
14-
public IActionResult Get()
14+
public void Get()
1515
{
1616
var registry = CollectorRegistry.Instance;
1717
var acceptHeaders = Request.Headers["Accept"];
1818
var contentType = ScrapeHandler.GetContentType(acceptHeaders);
1919
Response.ContentType = contentType;
20-
string content;
21-
22-
using (var outputStream = new MemoryStream())
20+
Response.StatusCode = 200;
21+
using (var outputStream = Response.Body)
2322
{
2423
var collected = registry.CollectAll();
2524
ScrapeHandler.ProcessScrapeRequest(collected, contentType, outputStream);
26-
content = Encoding.UTF8.GetString(outputStream.ToArray());
2725
}
28-
29-
return Ok(content);
3026
}
3127
}
3228
}

examples/WebCoreApplication/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"environmentVariables": {
2424
"ASPNETCORE_ENVIRONMENT": "Development"
2525
},
26-
"applicationUrl": "http://localhost:25049"
26+
"applicationUrl": "http://localhost:5000"
2727
}
2828
}
2929
}

0 commit comments

Comments
 (0)