Skip to content

Commit beb36e9

Browse files
committed
Added Web API sample
1 parent bc281ce commit beb36e9

File tree

11 files changed

+278
-1
lines changed

11 files changed

+278
-1
lines changed

Getting-Started/ASP.NET-Core-Web-API/Client Application/Client Application/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ static async Task Main(string[] args)
99
try
1010
{
1111
// Send a GET request to a URL
12-
HttpResponseMessage response = await client.GetAsync("http://localhost:5033/api/Values/api/Word");
12+
HttpResponseMessage response = await client.GetAsync("https://localhost:7152/api/Values/api/Word");
1313

1414
// Check if the response is successful
1515
if (response.IsSuccessStatusCode)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36518.9 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Create-Word-Document", "Create-Word-Document\Create-Word-Document.csproj", "{1175D1C7-30F6-41F9-8451-2FC500E37070}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{1175D1C7-30F6-41F9-8451-2FC500E37070}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{1175D1C7-30F6-41F9-8451-2FC500E37070}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{1175D1C7-30F6-41F9-8451-2FC500E37070}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{1175D1C7-30F6-41F9-8451-2FC500E37070}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {1B0A8A9C-7A9E-425C-A250-B70A4E89C5DB}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
using Microsoft.AspNetCore.Http;
2+
using Microsoft.AspNetCore.Mvc;
3+
using Syncfusion.DocIO;
4+
using Syncfusion.DocIO.DLS;
5+
using static System.Net.Mime.MediaTypeNames;
6+
7+
namespace Create_Word_Document.Controllers
8+
{
9+
[Route("api/[controller]")]
10+
[ApiController]
11+
public class ValuesController : ControllerBase
12+
{
13+
[HttpGet]
14+
[Route("api/Word")]
15+
public IActionResult CreateDocument()
16+
{
17+
try
18+
{
19+
var fileDownloadName = "Output.docx";
20+
const string contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
21+
var stream = GenerateWordDocument();
22+
stream.Position = 0;
23+
return File(stream, contentType, fileDownloadName);
24+
}
25+
catch (Exception ex)
26+
{
27+
// Log or handle the exception
28+
return BadRequest($"Error occurred while creating Word file: {ex.Message}");
29+
}
30+
}
31+
public static MemoryStream GenerateWordDocument()
32+
{
33+
//Creating a new document
34+
WordDocument document = new WordDocument();
35+
//Adding a new section to the document
36+
WSection section = document.AddSection() as WSection;
37+
//Set Margin of the section
38+
section.PageSetup.Margins.All = 72;
39+
//Set page size of the section
40+
section.PageSetup.PageSize = new Syncfusion.Drawing.SizeF(612, 792);
41+
42+
//Create Paragraph styles
43+
WParagraphStyle style = document.AddParagraphStyle("Normal") as WParagraphStyle;
44+
style.CharacterFormat.FontName = "Calibri";
45+
style.CharacterFormat.FontSize = 11f;
46+
style.ParagraphFormat.BeforeSpacing = 0;
47+
style.ParagraphFormat.AfterSpacing = 8;
48+
style.ParagraphFormat.LineSpacing = 13.8f;
49+
50+
style = document.AddParagraphStyle("Heading 1") as WParagraphStyle;
51+
style.ApplyBaseStyle("Normal");
52+
style.CharacterFormat.FontName = "Calibri Light";
53+
style.CharacterFormat.FontSize = 16f;
54+
style.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(46, 116, 181);
55+
style.ParagraphFormat.BeforeSpacing = 12;
56+
style.ParagraphFormat.AfterSpacing = 0;
57+
style.ParagraphFormat.Keep = true;
58+
style.ParagraphFormat.KeepFollow = true;
59+
style.ParagraphFormat.OutlineLevel = OutlineLevel.Level1;
60+
61+
IWParagraph paragraph = section.HeadersFooters.Header.AddParagraph();
62+
paragraph.ApplyStyle("Normal");
63+
paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left;
64+
WTextRange textRange = paragraph.AppendText("Adventure Works Cycles") as WTextRange;
65+
textRange.CharacterFormat.FontSize = 12f;
66+
textRange.CharacterFormat.FontName = "Calibri";
67+
textRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.Red;
68+
69+
//Appends paragraph
70+
paragraph = section.AddParagraph();
71+
paragraph.ApplyStyle("Heading 1");
72+
paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
73+
textRange = paragraph.AppendText("Adventure Works Cycles") as WTextRange;
74+
textRange.CharacterFormat.FontSize = 18f;
75+
textRange.CharacterFormat.FontName = "Calibri";
76+
77+
//Appends paragraph
78+
paragraph = section.AddParagraph();
79+
paragraph.ParagraphFormat.FirstLineIndent = 36;
80+
paragraph.BreakCharacterFormat.FontSize = 12f;
81+
textRange = paragraph.AppendText("Adventure Works Cycles, the fictitious company on which the AdventureWorks sample databases are based, is a large, multinational manufacturing company. The company manufactures and sells metal and composite bicycles to North American, European and Asian commercial markets. While its base operation is in Bothell, Washington with 290 employees, several regional sales teams are located throughout their market base.") as WTextRange;
82+
textRange.CharacterFormat.FontSize = 12f;
83+
84+
//Appends paragraph
85+
paragraph = section.AddParagraph();
86+
paragraph.ParagraphFormat.FirstLineIndent = 36;
87+
paragraph.BreakCharacterFormat.FontSize = 12f;
88+
textRange = paragraph.AppendText("In 2000, AdventureWorks Cycles bought a small manufacturing plant, Importadores Neptuno, located in Mexico. Importadores Neptuno manufactures several critical subcomponents for the AdventureWorks Cycles product line. These subcomponents are shipped to the Bothell location for final product assembly. In 2001, Importadores Neptuno, became the sole manufacturer and distributor of the touring bicycle product group.") as WTextRange;
89+
textRange.CharacterFormat.FontSize = 12f;
90+
91+
//Saving the Word document to the MemoryStream
92+
MemoryStream stream = new MemoryStream();
93+
document.Save(stream, FormatType.Docx);
94+
document.Close();
95+
//Set the position as '0'.
96+
stream.Position = 0;
97+
return stream;
98+
99+
}
100+
}
101+
102+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
3+
namespace Create_Word_Document.Controllers
4+
{
5+
[ApiController]
6+
[Route("[controller]")]
7+
public class WeatherForecastController : ControllerBase
8+
{
9+
private static readonly string[] Summaries = new[]
10+
{
11+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
12+
};
13+
14+
private readonly ILogger<WeatherForecastController> _logger;
15+
16+
public WeatherForecastController(ILogger<WeatherForecastController> logger)
17+
{
18+
_logger = logger;
19+
}
20+
21+
[HttpGet(Name = "GetWeatherForecast")]
22+
public IEnumerable<WeatherForecast> Get()
23+
{
24+
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
25+
{
26+
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
27+
TemperatureC = Random.Shared.Next(-20, 55),
28+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
29+
})
30+
.ToArray();
31+
}
32+
}
33+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<RootNamespace>Create_Word_Document</RootNamespace>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@Create_Word_Document_HostAddress = http://localhost:5098
2+
3+
GET {{Create_Word_Document_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var builder = WebApplication.CreateBuilder(args);
2+
3+
// Add services to the container.
4+
5+
builder.Services.AddControllers();
6+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
7+
builder.Services.AddEndpointsApiExplorer();
8+
builder.Services.AddSwaggerGen();
9+
10+
var app = builder.Build();
11+
12+
// Configure the HTTP request pipeline.
13+
if (app.Environment.IsDevelopment())
14+
{
15+
app.UseSwagger();
16+
app.UseSwaggerUI();
17+
}
18+
19+
app.UseHttpsRedirection();
20+
21+
app.UseAuthorization();
22+
23+
app.MapControllers();
24+
25+
app.Run();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:56490",
8+
"sslPort": 44367
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"applicationUrl": "http://localhost:5098",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
},
22+
"https": {
23+
"commandName": "Project",
24+
"dotnetRunMessages": true,
25+
"launchBrowser": true,
26+
"launchUrl": "swagger",
27+
"applicationUrl": "https://localhost:7152;http://localhost:5098",
28+
"environmentVariables": {
29+
"ASPNETCORE_ENVIRONMENT": "Development"
30+
}
31+
},
32+
"IIS Express": {
33+
"commandName": "IISExpress",
34+
"launchBrowser": true,
35+
"launchUrl": "swagger",
36+
"environmentVariables": {
37+
"ASPNETCORE_ENVIRONMENT": "Development"
38+
}
39+
}
40+
}
41+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Create_Word_Document
2+
{
3+
public class WeatherForecast
4+
{
5+
public DateOnly Date { get; set; }
6+
7+
public int TemperatureC { get; set; }
8+
9+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
10+
11+
public string? Summary { get; set; }
12+
}
13+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)