Skip to content

Instantly share code, notes, and snippets.

@puryfury
Created June 21, 2016 13:20
Show Gist options
  • Select an option

  • Save puryfury/2465e817ac4fd2c47b08135c5d03ef17 to your computer and use it in GitHub Desktop.

Select an option

Save puryfury/2465e817ac4fd2c47b08135c5d03ef17 to your computer and use it in GitHub Desktop.
ASP.NET Core MVC Static page route like ASP.NET Web Pages
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Example.Convention;
using Microsoft.Extensions.PlatformAbstractions;
namespace Example
{
public class Startup
{
public Startup(IHostingEnvironment env, IApplicationEnvironment app)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(app.ApplicationBasePath)
.AddJsonFile("appsettings.json")
.AddJsonFile("../config.json")
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppConfig>( Configuration.GetSection("AppConfig"));
var connection = Configuration["AppConfig:SQLConnectionString"];
services.AddEntityFramework()
.AddSqlServer();
//.AddDbContext<FileContext>(options => options.UseSqlServer(connection));
// Add framework services.
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationEnvironment root, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseIISPlatformHandler();
app.UseStaticFiles();
app.UseMvc(routes => {
// Static page route
routes.MapRoute(
"StaticContent", // Route name
"{*viewName}", // URL with parameters
new { controller = "StaticPage", action = "Page" }, // Parameter defaults
new { viewName = new StaticPageConstraint(root) } // Custom route constraint
);
});
}
// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}
}
using System.Collections.Generic;
using System.IO;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Routing;
using Microsoft.Extensions.PlatformAbstractions;
namespace Example.Convention
{
public class StaticPageConstraint : IRouteConstraint
{
IApplicationEnvironment env;
public StaticPageConstraint(IApplicationEnvironment env)
{
this.env = env;
}
public bool Match(HttpContext httpContext, IRouter route, string routeKey, IDictionary<string, object> values, RouteDirection routeDirection)
{
string viewPath = Path.Combine(env.ApplicationBasePath, $"Views/{values[routeKey]}.cshtml");
return File.Exists(viewPath);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment