Skip to content

Commit bc0b7c5

Browse files
committed
Added samples for WEB API in Getting started
1 parent 2ebd312 commit bc0b7c5

File tree

13 files changed

+332
-0
lines changed

13 files changed

+332
-0
lines changed
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}") = "Client Application", "Client Application\Client Application.csproj", "{CD420519-7EC6-4C93-97FA-5B5BBC0991A8}"
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+
{CD420519-7EC6-4C93-97FA-5B5BBC0991A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{CD420519-7EC6-4C93-97FA-5B5BBC0991A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{CD420519-7EC6-4C93-97FA-5B5BBC0991A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{CD420519-7EC6-4C93-97FA-5B5BBC0991A8}.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 = {8BC5181E-598C-415B-AB1F-77000335BC09}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Client_Application</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+

2+
class Program
3+
{
4+
static async Task Main(string[] args)
5+
{
6+
// Create an HttpClient instance
7+
using (HttpClient client = new HttpClient())
8+
{
9+
try
10+
{
11+
// Send a GET request to a URL
12+
HttpResponseMessage response = await client.GetAsync("http://localhost:5033/api/Values/api/Word");
13+
14+
// Check if the response is successful
15+
if (response.IsSuccessStatusCode)
16+
{
17+
// Read the content as a string
18+
Stream responseBody = await response.Content.ReadAsStreamAsync();
19+
FileStream fileStream = File.Create("../../../Output/Output.docx");
20+
responseBody.CopyTo(fileStream);
21+
fileStream.Close();
22+
}
23+
else
24+
{
25+
Console.WriteLine($"HTTP error status code: {response.StatusCode}");
26+
}
27+
}
28+
catch (HttpRequestException e)
29+
{
30+
Console.WriteLine($"Request exception: {e.Message}");
31+
}
32+
}
33+
}
34+
35+
}
36+
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", "{21AC2B42-4F00-422E-86C2-6528BE0F9D11}"
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+
{21AC2B42-4F00-422E-86C2-6528BE0F9D11}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{21AC2B42-4F00-422E-86C2-6528BE0F9D11}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{21AC2B42-4F00-422E-86C2-6528BE0F9D11}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{21AC2B42-4F00-422E-86C2-6528BE0F9D11}.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 = {80C45E24-85A9-49A0-B19E-5D691334A2FC}
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+
6+
namespace Create_Word_Document.Controllers
7+
{
8+
[Route("api/[controller]")]
9+
[ApiController]
10+
public class ValuesController : ControllerBase
11+
{
12+
[HttpGet]
13+
[Route("api/Word")]
14+
public IActionResult CreateDocument()
15+
{
16+
try
17+
{
18+
var fileDownloadName = $"Output.docx";
19+
const string contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
20+
var stream = GenerateWordDocument();
21+
stream.Position = 0;
22+
return File(stream, contentType, fileDownloadName);
23+
}
24+
catch (Exception ex)
25+
{
26+
// Log or handle the exception
27+
return BadRequest($"Error occurred while creating Excel file: {ex.Message}");
28+
}
29+
}
30+
public static MemoryStream GenerateWordDocument()
31+
{
32+
//Creating a new document
33+
WordDocument document = new WordDocument();
34+
//Adding a new section to the document
35+
WSection section = document.AddSection() as WSection;
36+
//Set Margin of the section
37+
section.PageSetup.Margins.All = 72;
38+
//Set page size of the section
39+
section.PageSetup.PageSize = new Syncfusion.Drawing.SizeF(612, 792);
40+
41+
//Create Paragraph styles
42+
WParagraphStyle style = document.AddParagraphStyle("Normal") as WParagraphStyle;
43+
style.CharacterFormat.FontName = "Calibri";
44+
style.CharacterFormat.FontSize = 11f;
45+
style.ParagraphFormat.BeforeSpacing = 0;
46+
style.ParagraphFormat.AfterSpacing = 8;
47+
style.ParagraphFormat.LineSpacing = 13.8f;
48+
49+
style = document.AddParagraphStyle("Heading 1") as WParagraphStyle;
50+
style.ApplyBaseStyle("Normal");
51+
style.CharacterFormat.FontName = "Calibri Light";
52+
style.CharacterFormat.FontSize = 16f;
53+
style.CharacterFormat.TextColor = Syncfusion.Drawing.Color.FromArgb(46, 116, 181);
54+
style.ParagraphFormat.BeforeSpacing = 12;
55+
style.ParagraphFormat.AfterSpacing = 0;
56+
style.ParagraphFormat.Keep = true;
57+
style.ParagraphFormat.KeepFollow = true;
58+
style.ParagraphFormat.OutlineLevel = OutlineLevel.Level1;
59+
60+
IWParagraph paragraph = section.HeadersFooters.Header.AddParagraph();
61+
paragraph.ApplyStyle("Normal");
62+
paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Left;
63+
WTextRange textRange = paragraph.AppendText("Adventure Works Cycles") as WTextRange;
64+
textRange.CharacterFormat.FontSize = 12f;
65+
textRange.CharacterFormat.FontName = "Calibri";
66+
textRange.CharacterFormat.TextColor = Syncfusion.Drawing.Color.Red;
67+
68+
//Appends paragraph
69+
paragraph = section.AddParagraph();
70+
paragraph.ApplyStyle("Heading 1");
71+
paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
72+
textRange = paragraph.AppendText("Adventure Works Cycles") as WTextRange;
73+
textRange.CharacterFormat.FontSize = 18f;
74+
textRange.CharacterFormat.FontName = "Calibri";
75+
76+
//Appends paragraph
77+
paragraph = section.AddParagraph();
78+
paragraph.ParagraphFormat.FirstLineIndent = 36;
79+
paragraph.BreakCharacterFormat.FontSize = 12f;
80+
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;
81+
textRange.CharacterFormat.FontSize = 12f;
82+
83+
//Appends paragraph
84+
paragraph = section.AddParagraph();
85+
paragraph.ParagraphFormat.FirstLineIndent = 36;
86+
paragraph.BreakCharacterFormat.FontSize = 12f;
87+
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;
88+
textRange.CharacterFormat.FontSize = 12f;
89+
90+
//Saving the Excel to the MemoryStream
91+
MemoryStream stream = new MemoryStream();
92+
document.Save(stream, FormatType.Docx);
93+
document.Close();
94+
95+
//Set the position as '0'.
96+
stream.Position = 0;
97+
98+
return stream;
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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.UseAuthorization();
20+
21+
app.MapControllers();
22+
23+
app.Run();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:17607",
8+
"sslPort": 0
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"applicationUrl": "http://localhost:5033",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
},
22+
"IIS Express": {
23+
"commandName": "IISExpress",
24+
"launchBrowser": true,
25+
"launchUrl": "swagger",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)