Migrate samples from hacky self host to helios

- MvcSample should be deleted and all new code should be written in MvcSample.Web
This commit is contained in:
David Fowler 2014-03-14 01:02:50 -07:00
parent 90e6897247
commit 930986d6a2
18 changed files with 476 additions and 21 deletions

View File

@ -0,0 +1,35 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
namespace MvcSample.Web.Filters
{
public class AgeEnhancerAttribute : ActionFilterAttribute
{
public async override Task Invoke(ActionFilterContext context, Func<Task> next)
{
object age = null;
if (context.ActionParameters.TryGetValue("age", out age))
{
if (age is int)
{
var intAge = (int) age;
if (intAge < 21)
{
intAge += 5;
}
else if (intAge > 30)
{
intAge = 29;
}
context.ActionParameters["age"] = intAge;
}
}
await next();
}
}
}

View File

@ -0,0 +1,27 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
using MvcSample.Web.Models;
namespace MvcSample.Web.Filters
{
public class InspectResultPageAttribute : ActionResultFilterAttribute
{
public async override Task Invoke(ActionResultFilterContext context, Func<Task> next)
{
ViewResult viewResult = context.Result as ViewResult;
if (viewResult != null)
{
User user = viewResult.ViewData.Model as User;
if (user != null)
{
user.Name += "**" + user.Name + "**";
}
}
await next();
}
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
namespace MvcSample.Web
{
public class PassThroughAttribute : AuthorizationFilterAttribute
{
public async override Task Invoke(AuthorizationFilterContext context, Func<Task> next)
{
await next();
}
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
namespace MvcSample.Web.Filters
{
public class UserNameProvider : ActionFilterAttribute
{
private static readonly string[] _userNames = new[] { "Jon", "David", "Goliath" };
private static int _index;
public override async Task Invoke(ActionFilterContext context, Func<Task> next)
{
object originalUserName = null;
context.ActionParameters.TryGetValue("userName", out originalUserName);
var userName = originalUserName as string;
if (string.IsNullOrWhiteSpace(userName))
{
context.ActionParameters["userName"] = _userNames[(_index++)%3];
}
await next();
}
}
}

View File

@ -0,0 +1,32 @@
using Microsoft.AspNet.Mvc;
using MvcSample.Web.Filters;
using MvcSample.Web.Models;
namespace MvcSample.Web
{
[ServiceFilter(typeof(PassThroughAttribute), Order = 1)]
[ServiceFilter(typeof(PassThroughAttribute))]
[PassThrough(Order = 0)]
[PassThrough(Order = 2)]
[InspectResultPage]
[UserNameProvider(Order = -1)]
public class FiltersController : Controller
{
private readonly User _user = new User() { Name = "User Name", Address = "Home Address" };
// TODO: Add a real filter here
[ServiceFilter(typeof(PassThroughAttribute))]
[AgeEnhancer]
public IActionResult Index(int age, string userName)
{
if (!string.IsNullOrEmpty(userName))
{
_user.Name = userName;
}
_user.Age = age;
return View("MyView", _user);
}
}
}

View File

@ -0,0 +1,56 @@
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.Mvc;
using MvcSample.Web.Models;
namespace MvcSample.Web.RandomNameSpace
{
public class Home2Controller
{
private User _user = new User() { Name = "User Name", Address = "Home Address" };
public Home2Controller(IActionResultHelper actionResultHelper)
{
Result = actionResultHelper;
}
public IActionResultHelper Result { get; private set; }
public HttpContext Context { get; set; }
public string Index()
{
return "Hello World: my namespace is " + this.GetType().Namespace;
}
public IActionResult Something()
{
return new ContentResult
{
Content = "Hello World From Content"
};
}
public IActionResult Hello()
{
return Result.Content("Hello World");
}
public void Raw()
{
Context.Response.WriteAsync("Hello World raw");
}
public IActionResult UserJson()
{
var jsonResult = Result.Json(_user);
jsonResult.Indent = false;
return jsonResult;
}
public User User()
{
return _user;
}
}
}

View File

@ -1,17 +1,63 @@
using Microsoft.AspNet.Mvc;
using MvcSample.Web.Models;
namespace MvcSample.Web
{
public class HomeController : Controller
{
public string Index()
public IActionResult Index()
{
return "Hello from the new MVC";
return View("MyView", User());
}
public IActionResult Test()
/// <summary>
/// Action that exercises query\form based model binding.
/// </summary>
public IActionResult SaveUser(User user)
{
return View();
return View("MyView", user);
}
/// <summary>
/// Action that exercises input formatter
/// </summary>
public IActionResult Post([FromBody]User user)
{
return View("MyView", user);
}
public IActionResult Something()
{
return new ContentResult
{
Content = "Hello World From Content"
};
}
public IActionResult Hello()
{
return Result.Content("Hello World");
}
public void Raw()
{
Context.Response.WriteAsync("Hello World raw");
}
public User User()
{
User user = new User()
{
Name = "My name",
Address = "My address"
};
return user;
}
public IActionResult MyView()
{
return View(User());
}
}
}

View File

@ -0,0 +1,33 @@

using Microsoft.AspNet.Mvc;
namespace MvcSample.Web
{
public class LinkController : Controller
{
public IActionResult Details()
{
return View();
}
public string About()
{
return Url.Action(null);
}
public string Get()
{
return Url.Route(new { controller = "Home", action = "Details" });
}
public string Link1()
{
return Url.Action("Index", "Home");
}
public string Link2()
{
return Url.Action("Link2");
}
}
}

View File

@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;
namespace MvcSample.Web.Models
{
public class User
{
[Required]
[MinLength(4)]
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
}

View File

@ -0,0 +1,30 @@

using Microsoft.AspNet.Mvc;
namespace MvcSample.Web
{
public class OverloadController
{
private readonly IActionResultHelper _result;
public OverloadController(IActionResultHelper result)
{
_result = result;
}
public IActionResult Get()
{
return _result.Content("Get()");
}
public IActionResult Get(int id)
{
return _result.Content("Get(id)");
}
public IActionResult Get(int id, string name)
{
return _result.Content("Get(id, name)");
}
}
}

View File

@ -0,0 +1,10 @@
namespace MvcSample.Web
{
public class SimplePocoController
{
public string Index()
{
return "Hello world";
}
}
}

View File

@ -0,0 +1,12 @@
using Microsoft.AspNet.Mvc;
namespace MvcSample.Web
{
public class SimpleRest : Controller
{
public string Get()
{
return "Get method";
}
}
}

View File

@ -1,21 +1,24 @@

#if NET45
using System;
using Autofac;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.ConfigurationModel;
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.DependencyInjection.Autofac;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Routing;
using Microsoft.Net.Runtime;
namespace MvcSample.Web
{
public class Startup
{
public void Configuration(IBuilder app)
public void Configuration(IBuilder builder)
{
var configuration = new Configuration();
var services = MvcServices.GetDefaultServices(configuration);
var serviceProvider =
DefaultServiceProvider.Create(app.ServiceProvider, services);
var containerBuilder = new ContainerBuilder();
var services = MvcServices.GetDefaultServices();
AutofacRegistration.Populate(containerBuilder, builder.ServiceProvider, services);
containerBuilder.RegisterInstance<PassThroughAttribute>(new PassThroughAttribute());
var serviceProvider = containerBuilder.Build().Resolve<IServiceProvider>();
var routes = new RouteCollection()
{
@ -30,7 +33,8 @@ namespace MvcSample.Web
"{controller}",
new { controller = "Home" });
app.UseRouter(routes);
builder.UseRouter(routes);
}
}
}
#endif

View File

@ -0,0 +1,25 @@
#if VIEWMETADATA
using System;
using System.Collections.Generic;
public class ViewMetadata
{
public static Dictionary<string, Type> Metadata
{
get
{
return new Dictionary<string, Type>
{
{
"~/Views/Home/MyView.cshtml",
typeof(MvcSample.Views.MyView)
},
{
"~/Views/Shared/_Layout.cshtml",
typeof(MvcSample.Views.Layout)
}
};
}
}
}
#endif

View File

@ -0,0 +1,3 @@

@Url.Action("About")

View File

@ -0,0 +1,36 @@
@using MvcSample.Web.Models
@model User
@{
Layout = "/Views/Shared/_Layout.cshtml";
ViewBag.Title = "Home Page";
string nullValue = null;
}
<div class="jumbotron">
<h1>ASP.NET</h1>
<p class="lead">ASP.NET is a free web framework for building great Web sites and Web applications using HTML, CSS and JavaScript.</p>
<p><a href="http://asp.net" class="btn btn-primary btn-large">Learn more &raquo;</a></p>
</div>
<div class="row">
<h3 title="@Model.Name" class="@nullValue">Hello @Model.Name! Happy @Model.Age birthday.</h3>
<div class="col-md-4">
<h2>Getting started</h2>
<p>
ASP.NET MVC gives you a powerful, patterns-based way to build dynamic websites that
enables a clean separation of concerns and gives you full control over markup
for enjoyable, agile development.
</p>
<p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301865">Learn more &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Get more libraries</h2>
<p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p>
<p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301866">Learn more &raquo;</a></p>
</div>
<div class="col-md-4">
<h2>Web Hosting</h2>
<p>You can easily find a web hosting company that offers the right mix of features and price for your applications.</p>
<p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301867">Learn more &raquo;</a></p>
</div>
</div>

View File

@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css" />
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="/">Home</a></li>
</ul>
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<address>
@Model.Address
</address>
<footer>
<p>&copy; @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
</body>
</html>

View File

@ -6,17 +6,31 @@
"Microsoft.AspNet.ConfigurationModel" : "0.1-alpha-*",
"Microsoft.AspNet.DependencyInjection" : "0.1-alpha-*",
"Microsoft.AspNet.Routing" : "0.1-alpha-*",
"Microsoft.AspNet.Mvc.Core" : "",
"Microsoft.AspNet.Mvc" : "",
"Microsoft.AspNet.Mvc.Razor" : ""
"Microsoft.AspNet.Mvc.ModelBinding": "",
"Microsoft.AspNet.Mvc.Core": "",
"Microsoft.AspNet.Mvc": "",
"Microsoft.AspNet.Mvc.Razor": "",
"Microsoft.AspNet.Mvc.Rendering": ""
},
"configurations": {
"net45": { },
"net45": {
"dependencies": {
"Autofac": "3.3.0",
"Microsoft.AspNet.DependencyInjection.Autofac": "0.1-alpha-*",
"System.ComponentModel.DataAnnotations": ""
}
},
"k10" : {
"dependencies": {
"System.Collections" : "4.0.0.0",
"System.Runtime" : "4.0.20.0",
"System.ComponentModel": "4.0.0.0"
"System.ComponentModel": "4.0.0.0",
"System.Console": "4.0.0.0",
"System.Diagnostics.Debug": "4.0.10.0",
"System.Diagnostics.Tools": "4.0.0.0",
"System.Dynamic.Runtime": "4.0.0.0",
"System.Runtime": "4.0.20.0",
"System.Runtime.InteropServices": "4.0.10.0",
"System.Threading.Tasks": "4.0.0.0",
"Microsoft.ComponentModel.DataAnnotations": "0.1-alpha-*"
}
}
}