Difference between revisions of ".NET Core MVC Linux Sample"

From no name for this wiki
Jump to: navigation, search
(MyController.json)
(MyController.cs)
Line 69: Line 69:
 
}
 
}
 
</source>
 
</source>
== MyController.cs ==
+
 
 +
 
 +
== Startup ==
 +
<source lang="csharp">
 +
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>();
 +
        }
 +
    }
 +
}
 +
</source>
 +
 
 +
 
 +
== MapController.cs ==
 
<source lang="csharp">
 
<source lang="csharp">
 
using Microsoft.AspNetCore.Mvc;
 
using Microsoft.AspNetCore.Mvc;

Revision as of 09:33, 17 September 2016

project.json

{
  "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
    }
  }
  
}

Programm.cs

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

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>();
        }
    }
}


MapController.cs

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);
        }
    }
}

Model

View