Difference between revisions of ".NET Core MVC Linux Sample"
(→Model) |
(→Creating a self singed certificate) |
||
(8 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | == project.json == | + | == Intro == |
+ | .net core MVC sample | ||
+ | |||
+ | == project.json (Directory .) == | ||
<source lang="javascript"> | <source lang="javascript"> | ||
Line 48: | Line 51: | ||
</source> | </source> | ||
− | == Programm.cs == | + | == appsettings.json (Directory .) == |
+ | <source lang="javascript"> | ||
+ | { | ||
+ | "MySettings": { | ||
+ | "ApplicationName": "My Very First MVC Application", | ||
+ | "MaxItemsPerList": 10 | ||
+ | }, | ||
+ | "Logging": { | ||
+ | "IncludeScopes": false, | ||
+ | "LogLevel": { | ||
+ | "Default": "Debug", | ||
+ | "System": "Error", | ||
+ | "Microsoft": "Error" | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | |||
+ | == Programm.cs (Directory .) == | ||
<source lang="csharp"> | <source lang="csharp"> | ||
using Microsoft.AspNetCore.Hosting; | using Microsoft.AspNetCore.Hosting; | ||
Line 71: | Line 93: | ||
− | == Startup == | + | == Startup.cs (Directory .) == |
<source lang="csharp"> | <source lang="csharp"> | ||
using System; | using System; | ||
Line 134: | Line 156: | ||
services.AddMvc(); | services.AddMvc(); | ||
services.AddSingleton<IOsmService, OsmService>(); | services.AddSingleton<IOsmService, OsmService>(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | == IOsmService.cs (Directory Services) == | ||
+ | <source lang="csharp"> | ||
+ | namespace aspnetcoreapp | ||
+ | { | ||
+ | public interface IOsmService { | ||
+ | MapInfo LoadCoordinates(); | ||
+ | } | ||
+ | } | ||
+ | </source> | ||
+ | |||
+ | == OsmService.cs (Directory Services) == | ||
+ | <source lang="csharp"> | ||
+ | namespace aspnetcoreapp | ||
+ | { | ||
+ | public class OsmService : IOsmService | ||
+ | { | ||
+ | public MapInfo LoadCoordinates() | ||
+ | { | ||
+ | MapInfo mapInfo = new MapInfo(){ | ||
+ | Longitude = "12.3455", | ||
+ | Latitude = "34.3344", | ||
+ | }; | ||
+ | return mapInfo; | ||
} | } | ||
} | } | ||
Line 197: | Line 247: | ||
</body> | </body> | ||
</html> | </html> | ||
+ | </source> | ||
+ | |||
+ | == Creating a self singed certificate == | ||
+ | openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 9999 | ||
+ | openssl pkcs12 -export -in cert.pem -inkey key.pem -out cert_key.p12 | ||
+ | |||
+ | == Loading == | ||
+ | <source lang="csharp"> | ||
+ | using Microsoft.AspNetCore.Hosting; | ||
+ | using System; | ||
+ | |||
+ | namespace aspnetcoreapp | ||
+ | { | ||
+ | public class Program | ||
+ | { | ||
+ | public static void Main(string[] args) | ||
+ | { | ||
+ | |||
+ | var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder(); | ||
+ | |||
+ | System.Security.Cryptography.X509Certificates.X509Certificate2 certificate | ||
+ | = new System.Security.Cryptography.X509Certificates.X509Certificate2("cert_key.p12","bla"); | ||
+ | |||
+ | if(!certificate.HasPrivateKey) | ||
+ | { | ||
+ | throw new System.Exception("Lacking pk"); | ||
+ | } | ||
+ | |||
+ | var host = new WebHostBuilder() | ||
+ | .UseKestrel(options => | ||
+ | options.UseHttps(certificate)) | ||
+ | .UseContentRoot(System.IO.Directory.GetCurrentDirectory()) | ||
+ | .UseUrls("http://*:4000/;https://*:44300/") | ||
+ | .UseStartup<Startup>() | ||
+ | .Build(); | ||
+ | |||
+ | host.Run(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
</source> | </source> |
Latest revision as of 14:33, 17 September 2016
Contents
- 1 Intro
- 2 project.json (Directory .)
- 3 appsettings.json (Directory .)
- 4 Programm.cs (Directory .)
- 5 Startup.cs (Directory .)
- 6 IOsmService.cs (Directory Services)
- 7 OsmService.cs (Directory Services)
- 8 MapController.cs (Directory Controllers)
- 9 MapInfo (Directory Models)
- 10 MyView.cshtml (Directory Views -> Map)
- 11 Creating a self singed certificate
- 12 Loading
Intro
.net core MVC sample
project.json (Directory .)
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable",
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"dependencies": {},
"frameworks": {
"netcoreapp1.0": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.0.0"
},
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0"
},
"imports": "dnxcore50",
"frameworkAssemblies": {
}
}
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
}
}
appsettings.json (Directory .)
{
"MySettings": {
"ApplicationName": "My Very First MVC Application",
"MaxItemsPerList": 10
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Error",
"Microsoft": "Error"
}
}
}
Programm.cs (Directory .)
using Microsoft.AspNetCore.Hosting;
namespace aspnetcoreapp
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(System.IO.Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}
Startup.cs (Directory .)
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
namespace aspnetcoreapp
{
public class Startup
{
IConfigurationRoot Configuration;
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddEnvironmentVariables();
builder.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
if (env.IsDevelopment())
{
}
Configuration = builder.Build();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton<IOsmService, OsmService>();
}
}
}
IOsmService.cs (Directory Services)
namespace aspnetcoreapp
{
public interface IOsmService {
MapInfo LoadCoordinates();
}
}
OsmService.cs (Directory Services)
namespace aspnetcoreapp
{
public class OsmService : IOsmService
{
public MapInfo LoadCoordinates()
{
MapInfo mapInfo = new MapInfo(){
Longitude = "12.3455",
Latitude = "34.3344",
};
return mapInfo;
}
}
}
MapController.cs (Directory Controllers)
using Microsoft.AspNetCore.Mvc;
namespace aspnetcoreapp
{
public class MapController : Controller {
IOsmService _osmService;
public MapController(IOsmService osmService){
_osmService = osmService;
}
public string GetString() {
return "Hello World from Controller!";
}
public ActionResult MyView()
{
MapInfo model = _osmService.LoadCoordinates();
return View("MyView", model);
}
}
}
MapInfo (Directory Models)
namespace aspnetcoreapp
{
public class MapInfo {
public string Latitude {get; set;}
public string Longitude {get;set;}
}
}
MyView.cshtml (Directory Views -> Map)
@model aspnetcoreapp.MapInfo
@{
ViewData["Title"] = "Coordinates";
}
<html>
<body>
<h2>@ViewData["Title"]</h2>
<p>
<ul>
<li>Longitude: @Html.DisplayFor(x => x.Longitude)</li>
<li>Longitude: @Html.DisplayFor(x => x.Latitude)</li>
</ul>
</p>
</body>
</html>
Creating a self singed certificate
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 9999 openssl pkcs12 -export -in cert.pem -inkey key.pem -out cert_key.p12
Loading
using Microsoft.AspNetCore.Hosting;
using System;
namespace aspnetcoreapp
{
public class Program
{
public static void Main(string[] args)
{
var config = new Microsoft.Extensions.Configuration.ConfigurationBuilder();
System.Security.Cryptography.X509Certificates.X509Certificate2 certificate
= new System.Security.Cryptography.X509Certificates.X509Certificate2("cert_key.p12","bla");
if(!certificate.HasPrivateKey)
{
throw new System.Exception("Lacking pk");
}
var host = new WebHostBuilder()
.UseKestrel(options =>
options.UseHttps(certificate))
.UseContentRoot(System.IO.Directory.GetCurrentDirectory())
.UseUrls("http://*:4000/;https://*:44300/")
.UseStartup<Startup>()
.Build();
host.Run();
}
}
}